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 })));
}
}
}