gpt4 book ai didi

asp.net - ASP.Net WebAPI 中的自定义路由处理程序

转载 作者:行者123 更新时间:2023-12-02 03:30:56 29 4
gpt4 key购买 nike

我能够在 global.asax.cs 中为 Web API 路由 ala 成功注册自定义路由处理程序(从 IRouteHandler 派生):

        routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{client}/api/1.0/{controller}/{action}/{id}",
defaults: new{id = UrlParameter.Optional}
).RouteHandler = new SingleActionAPIRouteHandler();

但是,当我尝试为 API 设置内存主机以进行集成测试时,我似乎找不到方法来执行此操作,当我调用 HttpConfiguration.Routes.MapRoute 时,我无法在返回 IHttpRoute。

我应该以不同的方式执行此操作(例如使用自定义 HttpControllerSelector)吗?显然,我希望在这两种情况下都以相同的方式进行操作。

谢谢,马特

编辑:

所以我最终所做的基本上是遵循下面的建议,但只是重写 HttpControllerDispatcher 类,如下所示:

public class CustomHttpControllerDispatcher : HttpControllerDispatcher
{
public CustomHttpControllerDispatcher(HttpConfiguration configuration) : base(configuration)
{
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// My stuff here

return base.SendAsync(request, cancellationToken);
}
}

最佳答案

你说得非常对。自托管返回 IHttpRoute 并以 HttpMessageHandler 作为参数。似乎没有内置的方法来指定路由处理程序。

更新:更清晰一点

您几乎肯定应该尝试使用 HttpControllerSelector 并实现自定义的......一个例子是。 http://netmvc.blogspot.co.uk/

接下来是一些实验,如果 HttpControllerSelector 出于某种原因无法满足您的要求......

但是,正如您所看到的,MapHttpRoute 确实有 HttpMessageHandler 的重载,因此您可以尝试一下... if the handler is NULL then it defaults to IHttpController但您可以实现自己的并使用它来定向到正确的 Controller ... MVC 框架似乎使用 [HttpControllerDispatcher] here ,因此借用一些代码,您也可以在这里放置自己的 Controller /路由选择代码 - 您可以访问路由和选择器,并且可以自己将其换入换出。

此 CustomHttpControllerDispatcher 代码仅用于演示...查找该行

//DO SOMETHING CUSTOM HERE TO PICK YOUR CONTROLLER

也许可以玩一下......

路线示例:

        config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: null,
handler: new CustomHttpControllerDispatcher(config)
);

示例 CustomHttpControllerDispatcher:

public class CustomHttpControllerDispatcher : HttpMessageHandler
{
private IHttpControllerSelector _controllerSelector;
private readonly HttpConfiguration _configuration;

public CustomHttpControllerDispatcher(HttpConfiguration configuration)
{
_configuration = configuration;
}

public HttpConfiguration Configuration
{
get { return _configuration; }
}

private IHttpControllerSelector ControllerSelector
{
get
{
if (_controllerSelector == null)
{
_controllerSelector = _configuration.Services.GetHttpControllerSelector();
}
return _controllerSelector;
}
}

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return SendAsyncInternal(request, cancellationToken);
}

private Task<HttpResponseMessage> SendAsyncInternal(HttpRequestMessage request, CancellationToken cancellationToken)
{

IHttpRouteData routeData = request.GetRouteData();
Contract.Assert(routeData != null);

//DO SOMETHING CUSTOM HERE TO PICK YOUR CONTROLLER
HttpControllerDescriptor httpControllerDescriptor = ControllerSelector.SelectController(request);
IHttpController httpController = httpControllerDescriptor.CreateController(request);

// Create context
HttpControllerContext controllerContext = new HttpControllerContext(_configuration, routeData, request);
controllerContext.Controller = httpController;
controllerContext.ControllerDescriptor = httpControllerDescriptor;

return httpController.ExecuteAsync(controllerContext, cancellationToken);
}
}

关于asp.net - ASP.Net WebAPI 中的自定义路由处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12371269/

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