C# 使用 ASPNET MVC 4 和 webapi 自定义 httphandler 和 routehandler
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13593114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom httphandler and routehandler with ASPNET MVC 4 and webapi
提问by polonskyg
I'm working on an ASPNET MVC 4 and WebApi. The webapi methods will be consumed by mobile devices. We need to secure the services and what we are using is to encrypt the data in some particular way.
我正在研究 ASPNET MVC 4 和 WebApi。webapi 方法将由移动设备使用。我们需要保护服务,我们使用的是以某种特定方式加密数据。
Now, I need to decrypt the call before the controller is reached. If the information decrypted is valid, it should continue to the controller as usual if not, I'll route the user to some error method.
现在,我需要在到达控制器之前解密调用。如果解密的信息有效,它应该像往常一样继续到控制器,如果不是,我会将用户路由到某些错误方法。
To accomplish this I think the best bet would be custom HttpHandler and custom RouteHandler. I'm following the tutorial here
为了实现这一点,我认为最好的选择是自定义 HttpHandler 和自定义 RouteHandler。我正在关注这里的教程
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.cs
Global.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);
}
and in Application_Start
并在 Application_Start 中
RegisterRoutes(RouteTable.Routes);
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?
服务启动后,我在 ProcessRequest 中创建了一个断点并且它没有被命中。可能缺少什么?这是正确的做法吗?
采纳答案by trebuchet
If you haven't yet, you need to first register the handler in global.asax or your web.config file.
如果您还没有,您需要先在 global.asax 或您的 web.config 文件中注册处理程序。
http://msdn.microsoft.com/en-us/library/46c5ddfy(v=vs.100).aspx
http://msdn.microsoft.com/en-us/library/46c5ddfy(v=vs.100).aspx

