HttpModule
確實爲此太早,實際上並不需要。你可以依靠常規的MVC路由處理機制,並簡單地提供你自己的從HTTP請求中提取的RouteValues
。
例如:
public class MyHeadersBasedRoute : RouteBase
{
public const string HEADER_CONTROLLER_KEY = "X-REQUESTED-CONTROLLER";
public const string HEADER_ACTION_KEY = "X-REQUESTED-ACTION";
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var requestedController = httpContext.Request.Headers[HEADER_CONTROLLER_KEY];
var requestedAction = httpContext.Request.Headers[HEADER_ACTION_KEY];
if (String.IsNullOrEmpty(requestedController) || String.IsNullOrEmpty(requestedAction))
return null;
var ret = new RouteData(this, new MvcRouteHandler());
ret.Values.Add("controller", requestedController);
ret.Values.Add("action", requestedAction);
// add any extra parameter from request, for example:
ret.Values.Add("id", httpContext.Request.Form["id"]);
return ret;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
return null;
}
}
然後,只需註冊在你global.asax
:
RouteTable.Routes.Add(new MyHeadersBasedRoute());
完美。謝謝! –