我们为客户提供目录,并为他们提供可以使用的URL:
www.ourhost.com/customername
使用我们开发的自定义IHttpModule,我们从URL中提取“customername”以在数据库中查找客户.然后,该客户的ID将存储在页面的上下文*中,并由网站上的几乎所有页面使用,以便为该客户自定义内容.完成此过程后,上述URL将被重写并处理为
www.ourhost.com/index.aspx
index.aspx可以通过其上下文访问客户的ID,它可以做到这一点.
这很好用,我们用它支持数千名客户.重写逻辑相当复杂,因为它验证了客户帐户,如果客户无效,则重定向到“哦”页面,如果客户没有付款,则重定向到“查找经销商”页面等.
现在我想构建一些Web API控制器,MVC风格的重写令我担心.我看到很多例子,其中重写恰好使URL像这样工作:
www.ourhost.com/api/{controller}
但我仍然需要在客户的环境中发生这些web api’调用’.我们的页面使用JSON / AJAX异步调用变得越来越复杂,但在回答这些调用时,我仍然需要客户上下文.我想要的URL是
www.ourhost.com/customername/api/{controller}
但我很困惑如何配置路由来做到这一点,并让它与我们的IHttpModule很好地配合.
这甚至可能吗?
*更新:当我说“存储在页面上下文中”时,我指的是与每个Web请求相关联的HttpContext,其中包含一个字典,我可以在其中存储一些页面/请求特定的数据.
解决方法
跨多个请求维护用户信息
通常,MVC API应用程序将是无状态的,即您不会在请求之间保留当前用户会话状态.这就是我在编写RESTFul API时多次学习或传播的内容.
话虽如此,您可以通过在global.asax.cs中添加以下内容来启用MVC Web API中的会话状态
protected void Application_PostAuthorizeRequest() { // To enable session state in the WebAPI. System.Web.HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.required); }
在请求中授权客户
正如您在请求URL中所示,您可以添加客户名称,然后将其捕获并传递给当前http模块根据请求进行授权调用的相同例程.您可以使用MVC过滤器执行此操作.
首先做一个类似的URL模式来捕获WebApiConfig.cs中的客户名称,就像这样;
config.Routes.MapHttpRoute( name: "WithCustomerApi",routeTemplate: "api/{customername}/{controller}/{id}",defaults: new { id = RouteParameter.Optional } );
然后向您的API控制器添加一个ActionFilter,它处理每个请求,检查当前会话信息,如果需要,调用您的授权/客户查找代码,然后保存到会话状态供以后使用.或者,如果客户没有好的信息可以发送到新的MVC路线
[WebApiAuthentication] public class BaseApiController : ApiController { }
然后创建一个可能看起来像这样的动作过滤器(注意我没有测试过这个,只是针对如何做的模式).
public class WebApiAuthenticationAttribute : ActionFilterattribute { public override void OnActionExecuting(HttpActionContext actionContext) { var routeData = actionContext.ControllerContext.Request.GetRouteData(); var currentContext = HttpContext.Current; if (routeData.Route.RouteTemplate.Contains("customername")) { try { var authenticated = currentContext.Request.IsAuthenticated; if (!authenticated) { var customer = routeData.Values["customername"]; // do something with customer here and then put into session or cache currentContext.Session.Add("CustomerName",customer); } } catch (Exception exception) { var error = exception.Message; // We dont like the request actionContext.Response = new HttpResponseMessage(HttpStatusCode.BadRequest); } } else { // No customer name specified,send bad request,not found,what have you ... you *Could* potentially redirect but we are in API so it probably a service request rather than a user actionContext.Response = new HttpResponseMessage(HttpStatusCode.NotFound); } } }
如果您创建一个新的MVC 5 Web API应用程序并添加这些额外内容并将过滤器放在默认值控制器上,那么您应该能够看到它作为可能解决方案的演示运行.
如果一切正常,这将回显客户名称.
[WebApiAuthentication] public class ValuesController : ApiController { // GET api/values public IEnumerable<string> Get() { var session = HttpContext.Current.Session; if (session != null) { return new string[] {"session is present","customer is",session["CustomerName"].ToString()}; } return new string[] { "value1","value2" }; } }
我提出这个可能的解决方案就像我说的那样,存在关于在API中存储会话和授权的宗教论点,但这些都不是问题.希望有所帮助,史蒂夫