OData& WebAPI路由冲突

时间:2015-08-20 12:55:45

标签: asp.net-mvc asp.net-web-api routing odata

我有一个带有WebAPI控制器的项目。我现在正在为它添加OData控制器。问题是我的OData控制器与现有的WebAPI控制器同名,这导致异常:

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

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

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

在我看来,网址足够明显,以至于某些方式配置路由,因此没有冲突。

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

2 个答案:

答案 0 :(得分:0)

您需要在WebAPI上包含名称空间约束:

var route = context.Routes.MapHttpRoute(
            name: "API_default",
            routeTemplate: "API/{controller}/{action}/{id}",
            defaults:new { action = RouteParameter.Optional, id = RouteParameter.Optional },
        );
route.DataTokens["Namespaces"] = new string[] {"Foo.Bar.Web.Areas.API.Controllers"];

如果您遇到视图控制器的冲突,您应该能够包含类似的命名空间约束:

routes.MapRoute(
            name: "ViewControllers_Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, area = "" },
            namespaces: new[]{"Foo.Bar.Web.Controllers"}
        );

答案 1 :(得分:0)

如果apiOData有两个具有相同名称和不同名称空间的控制器,则可以使用此代码。首先添加此类:

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,您应该在Register文件内WebApiConfig.cs方法的末尾添加以下行:

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

注释:

它使用控制器的名称空间来确定控制器是否为OData。因此,对于OData控制器,您应该有namespace YourProject.Controllers.OData;对于API控制器,您应该有OData。在命名空间中,它不应包含<form id="autoForm" autocomplete="off"> <div class="autocomplete" style="width:300px;"> <input type="text" name="hotelName" id="hotelName" placeholder="Hotel Name"> </div> </form> <button onclick="hotelSelection()">Send</button> 字。

感谢Martin Devillers的帖子。我用了他的想法和一段代码!