gpt4 book ai didi

c# - 使用 ASP NET MVC 4 和 webapi 的自定义 http 处理程序和路由处理程序

转载 作者:可可西里 更新时间:2023-11-01 09:11:03 25 4
gpt4 key购买 nike

我正在研究 ASPNET MVC 4 和 WebApi。移动设备将使用 webapi 方法。我们需要保护服务,我们正在使用的是以某种特定方式加密数据。

现在,我需要在到达 Controller 之前解密调用。如果解密的信息有效,它应该像往常一样继续到 Controller ,如果不是,我会将用户路由到一些错误方法。

要实现这一点,我认为最好的办法是自定义 HttpHandler 和自定义 RouteHandler。我正在学习教程 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
});
}
}

全局.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_Start 中

RegisterRoutes(RouteTable.Routes);

服务启动后,我在 ProcessRequest 中创建了一个断点,它没有被命中。可能缺少什么?这是正确的做法吗?

最佳答案

如果您还没有,您需要先在 global.asax 或您的 web.config 文件中注册处理程序。

http://msdn.microsoft.com/en-us/library/46c5ddfy(v=vs.100).aspx

关于c# - 使用 ASP NET MVC 4 和 webapi 的自定义 http 处理程序和路由处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13593114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com