webapi 路由限制命名控件_解决Web API路由配置支持Area及命名空间参数

这个博客文章详细介绍了如何通过自定义NamespaceHttpControllerSelector类来解决ASP.NET MVC和Web API中控制器类名相同的冲突问题。作者提供了一个实现,该实现能够根据请求的命名空间来选择正确的控制器,确保系统能够正确路由到相应的控制器实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

usingSystem;usingSystem.Collections.Concurrent;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Net;usingSystem.Net.Http;usingSystem.Web;usingSystem.Web.Http;usingSystem.Web.Http.Controllers;usingSystem.Web.Http.Dispatcher;namespace_1_解决MVC的Controller和Web_API的Controller类名不能相同的问题.App_Start.webExt

{public classNamespaceHttpControllerSelector : DefaultHttpControllerSelector

{private const string NamespaceRouteVariableName = "namespaceName";private readonlyHttpConfiguration _configuration;private readonly Lazy>_apiControllerCache;publicNamespaceHttpControllerSelector(HttpConfiguration configuration)

:base(configuration)

{

_configuration=configuration;

_apiControllerCache= new Lazy>(new Func>(InitializeApiControllerCache));

}private ConcurrentDictionaryInitializeApiControllerCache()

{

IAssembliesResolver assembliesResolver= this._configuration.Services.GetAssembliesResolver();var types = this._configuration.Services.GetHttpControllerTypeResolver().GetControllerTypes(assembliesResolver).ToDictionary(t => t.FullName, t =>t);return new ConcurrentDictionary(types);

}public IEnumerable GetControllerFullName(HttpRequestMessage request, stringcontrollerName)

{objectnamespaceName;var data =request.GetRouteData();

IEnumerable keys = _apiControllerCache.Value.ToDictionary, string, Type>(t =>t.Key,

t=>t.Value, StringComparer.CurrentCultureIgnoreCase).Keys.ToList();if (!data.Values.TryGetValue(NamespaceRouteVariableName, outnamespaceName))

{return from k inkeyswhere k.EndsWith(string.Format(".{0}{1}", controllerName, DefaultHttpControllerSelector.ControllerSuffix), StringComparison.CurrentCultureIgnoreCase)selectk;

}//get the defined namespace

string[] namespaces = (string[])namespaceName;return from n innamespaces

join kin keys on string.Format("{0}.{1}{2}", n, controllerName, DefaultHttpControllerSelector.ControllerSuffix).ToLower() equals k.ToLower()selectk;

}public overrideHttpControllerDescriptor SelectController(HttpRequestMessage request)

{

Type type;if (request == null)

{throw new ArgumentNullException("request");

}string controllerName = this.GetControllerName(request);if (string.IsNullOrEmpty(controllerName))

{throw newHttpResponseException(request.CreateErrorResponse(HttpStatusCode.NotFound,string.Format("No route providing a controller name was found to match request URI '{0}'", new object[] { request.RequestUri })));

}

IEnumerable fullNames =GetControllerFullName(request, controllerName);if (fullNames.Count() == 0)

{throw newHttpResponseException(request.CreateErrorResponse(HttpStatusCode.NotFound,string.Format("No route providing a controller name was found to match request URI '{0}'", new object[] { request.RequestUri })));

}if (this._apiControllerCache.Value.TryGetValue(fullNames.First(), outtype))

{return newHttpControllerDescriptor(_configuration, controllerName, type);

}throw newHttpResponseException(request.CreateErrorResponse(HttpStatusCode.NotFound,string.Format("No route providing a controller name was found to match request URI '{0}'", new object[] { request.RequestUri })));

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值