我想在我的api中再添加一个参数,但它没有修复。在这种情况下,{config}可以是config1,config2,config1433 ....
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{config}/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
我已经尝试使用委托处理程序获取参数并将其从请求uri中删除,但它不起作用,因为已经分析了网址。
答案 0 :(得分:0)
我找到了解决方案。
首先,我必须从DefaultHttpControllerSelector继承。派生此类允许您自定义控制器选择。就我而言,就是那么简单。
public class CustomControllerSelector : DefaultHttpControllerSelector
{
private HttpConfiguration _config;
public IHttpControllerSelector PreviousSelector { get; set; }
public CustomControllerSelector(HttpConfiguration configuration):base(configuration)
{
_config = configuration;
}
public override HttpControllerDescriptor SelectController(HttpRequestMessage request)
{
var uriSegments = request.RequestUri.Segments;
var system = uriSegments[2];
request.RequestUri = new System.Uri(request.RequestUri.AbsoluteUri.Replace(system, ""));
var controllerDescriptor = PreviousSelector.SelectController(request);
return controllerDescriptor;
}
}
我不得不用我的自定义替换默认值。但我保存了前一个,因为我在网址操作后使用了它。
var previousSelector = config.Services.GetService(typeof(IHttpControllerSelector)) as IHttpControllerSelector;
config.Services.Replace(typeof(IHttpControllerSelector), new CustomControllerSelector(config) { PreviousSelector = previousSelector});
这是我的参考。
ASP.NET Web API custom IHttpControllerSelector for a single route