gpt4 book ai didi

asp.net-mvc - OData 和 Web API 路由冲突

转载 作者:行者123 更新时间:2023-12-02 01:26:32 27 4
gpt4 key购买 nike

我有一个带有 WebAPI Controller 的项目。我现在正在向其中添加 OData Controller 。问题是我的 OData Controller 与现有的 WebAPI Controller 具有相同的名称,这会导致异常:

Multiple types were found that match the controller named 'Member'. This can happen if the route that services this request ('OData/{*odataPath}') found multiple controllers defined with the same name but differing namespaces, which is not supported. The request for 'Member' has found the following matching controllers: Foo.Bar.Web.Areas.API.Controllers.MemberController Foo.Bar.Web.Odata.Controllers.MemberController

即使 Controller 位于不同的命名空间并且应该具有可区分的路由,也会发生这种情况。这是我所拥有的配置的摘要。我可以做什么(除了重命名 Controller )来防止此异常?我正在尝试将这些端点公开为:

mysite.com/OData/Members
mysite.com/API/Members/EndPoint

在我看来,URL 足够独特,必须有某种方式来配置路由,这样就不会发生冲突。

namespace Foo.Bar.Web.Odata.Controllers {

public class MemberController : ODataController {
[EnableQuery]
public IHttpActionResult Get() {
// ... do stuff with EF ...
}
}
}

namespace Foo.Bar.Web.Areas.API.Controllers {

public class MemberController : ApiControllerBase {
[HttpPost]
public HttpResponseMessage EndPoint(SomeModel model) {
// ... do stuff to check email ...
}
}
}

public class FooBarApp : HttpApplication {

protected void Application_Start () {
// ... snip ...

GlobalConfiguration.Configure(ODataConfig.Register);
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);

// ... snip ...
}
}

public static class ODataConfig {
public static void Register(HttpConfiguration config) {
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: "OData",
model: GetModel());
}

public static Microsoft.OData.Edm.IEdmModel GetModel() {
// ... build edm models ...
}
}

namespace Foo.Bar.Web.Areas.API {
public class APIAreaRegistration : AreaRegistration {
public override string AreaName {
get { return "API"; }
}

public override void RegisterArea(AreaRegistrationContext context) {
var route = context.Routes.MapHttpRoute(
"API_default",
"API/{controller}/{action}/{id}",
new { action = RouteParameter.Optional, id = RouteParameter.Optional }
);
}
}
}

最佳答案

如果您有两个具有相同名称且 apiOData 具有不同命名空间的 Controller ,则可以使用此代码。首先添加这个类:

public class ODataHttpControllerSelector : DefaultHttpControllerSelector
{
private readonly HttpConfiguration _configuration;
private readonly Lazy<ConcurrentDictionary<string, Type>> _apiControllerTypes;

public ODataHttpControllerSelector(HttpConfiguration configuration)
: base(configuration)
{
_configuration = configuration;
_apiControllerTypes = new Lazy<ConcurrentDictionary<string, Type>>(GetControllerTypes);
}

public override HttpControllerDescriptor SelectController(HttpRequestMessage request)
{
return this.GetApiController(request);
}

private static ConcurrentDictionary<string, Type> GetControllerTypes()
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();

var types = assemblies
.SelectMany(a => a
.GetTypes().Where(t =>
!t.IsAbstract &&
t.Name.EndsWith(ControllerSuffix, StringComparison.OrdinalIgnoreCase) &&
typeof(IHttpController).IsAssignableFrom(t)))
.ToDictionary(t => t.FullName, t => t);

return new ConcurrentDictionary<string, Type>(types);
}

private HttpControllerDescriptor GetApiController(HttpRequestMessage request)
{
var isOData = IsOData(request);
var controllerName = GetControllerName(request);
var type = GetControllerType(isOData, controllerName);

return new HttpControllerDescriptor(_configuration, controllerName, type);
}

private static bool IsOData(HttpRequestMessage request)
{
var data = request.RequestUri.ToString();
bool match = data.IndexOf("/OData/", StringComparison.OrdinalIgnoreCase) >= 0 ||
data.EndsWith("/OData", StringComparison.OrdinalIgnoreCase);
return match;
}

private Type GetControllerType(bool isOData, string controllerName)
{
var query = _apiControllerTypes.Value.AsEnumerable();

if (isOData)
{
query = query.FromOData();
}
else
{
query = query.WithoutOData();
}

return query
.ByControllerName(controllerName)
.Select(x => x.Value)
.Single();
}
}

public static class ControllerTypeSpecifications
{
public static IEnumerable<KeyValuePair<string, Type>> FromOData(this IEnumerable<KeyValuePair<string, Type>> query)
{
return query.Where(x => x.Key.IndexOf(".OData.", StringComparison.OrdinalIgnoreCase) >= 0);
}

public static IEnumerable<KeyValuePair<string, Type>> WithoutOData(this IEnumerable<KeyValuePair<string, Type>> query)
{
return query.Where(x => x.Key.IndexOf(".OData.", StringComparison.OrdinalIgnoreCase) < 0);
}

public static IEnumerable<KeyValuePair<string, Type>> ByControllerName(this IEnumerable<KeyValuePair<string, Type>> query, string controllerName)
{
var controllerNameToFind = string.Format(CultureInfo.InvariantCulture, ".{0}{1}", controllerName, DefaultHttpControllerSelector.ControllerSuffix);

return query.Where(x => x.Key.EndsWith(controllerNameToFind, StringComparison.OrdinalIgnoreCase));
}
}

它驱动 DefaultHttpControllerSelector,您应该在 WebApiConfig.cs 文件内的 Register 方法的末尾添加此行:

config.Services.Replace(typeof(IHttpControllerSelector), new ODataHttpControllerSelector(config));

注释:

它使用 Controller 的命名空间来确定 Controller 是否为 OData。因此,您的 OData Controller 应该有命名空间 YourProject.Controllers.OData,而对于 API Controller ,它不应在命名空间中包含 OData 单词。

感谢Martin Devillers为了他的职位。我使用了他的想法和他的一段代码!

关于asp.net-mvc - OData 和 Web API 路由冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32118856/

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