To accomplish this I think the best bet would be custom HttpHandler and custom RouteHandler. I'm following the tutorial here public class MvcSecurityRouteHandler:IRouteHandler { public System.Web.IHttpHandler GetHttpHandler(RequestContext requestContext) { return new MvcSecurityHttpHandler(requestContext); } }public class MvcSecurityHttpHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState, IRouteHandler { public RequestContext RequestContext { get; set; } public MvcSecurityHttpHandler(RequestContext requestContext) { this.RequestContext = requestContext; } public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext httpContext) { var controllerId = RequestContext.RouteData.GetRequiredString("controllerId"); IController controller = null; IControllerFactory factory = null; try { factory = ControllerBuilder.Current.GetControllerFactory(); controller = factory.CreateController(RequestContext, controllerId); if (controller != null) { controller.Execute(RequestContext); } } finally { factory.ReleaseController(controller); } //string originalPath = httpContext.Request.Path; //HttpContext.Current.RewritePath(httpContext.Request.ApplicationPath, false); //IHttpHandler httpHandler = new MvcHttpHandler(); //httpHandler.ProcessRequest(HttpContext.Current); //HttpContext.Current.RewritePath(originalPath, false); } public IHttpHandler GetHttpHandler(RequestContext requestContext) { throw new NotImplementedException(); } }public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); var defaults = new RouteValueDictionary {{"controllerId", "Home"},{"action", "Index"},{"id", string.Empty}}; var customRoute = new Route("{controllerId}/{action}/{id}", defaults, new MvcSecurityRouteHandler()); routes.Add(customRoute); routes.MapRoute( name: "DefaultWebApi", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }); } }Global.asax.csGlobal.asax.cs public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); var defaults = new RouteValueDictionary {{"controllerId", "Home"},{"action", "Index"},{"id", string.Empty}}; var customRoute = new Route("{controllerId}/{action}/{id}", defaults, new MvcSecurityRouteHandler()); routes.Add(customRoute); }并在 Application_Startand in Application_StartRegisterRoutes(RouteTable.Routes);服务启动后,我在 ProcessRequest 中创建了一个断点并且它没有被命中.可能缺少什么?这是正确的做法吗?After the service is up, I create a breakpoint in ProcessRequest and it is not being hit.What could be missing? Is this the correct way of doing it?推荐答案如果您还没有,您需要先在 global.asax 或您的 web.config 文件中注册处理程序.If you haven't yet, you need to first register the handler in global.asax or your web.config file.http://msdn.microsoft.com/en-us/library/46c5ddfy(v=vs.100).aspx 这篇关于带有 ASPNET MVC 4 和 webapi 的自定义 httphandler 和 routehandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-07 05:37