简体   繁体   English

在不同NameSpace ASP.NET WEB API中具有相同名称的控制器

[英]Controllers With Same Name In Different NameSpace ASP.NET WEB API

I need to have controllers with same name in different namespace. 我需要在不同的名称空间中使用具有相同名称的控制器。 The controllers I'm having are: 我拥有的控制器是:

namespace BSB.Messages.Controllers.V1
{    
    public class MessagesController : ApiController {...}
}

namespace BSB.Messages.Controllers.V2
{       
    public class MessagesController : ApiController {...}
}

I tried to configure it in start up. 我试图在启动时对其进行配置。 But still when I make a call it shows error that: 但是,当我拨打电话时,它仍然显示以下错误:

Multiple types were found that match the controller named 'messages'. 发现了多个与名为“ messages”的控制器匹配的类型。 This can happen if the route that services this request ('api/{namespace}/{controller}/{action}/{id}') found multiple controllers defined with the same name but differing namespaces, which is not supported 如果为该请求提供服务的路由('api / {namespace} / {controller} / {action} / {id}')发现多个定义了相同名称但名称空间不同的控制器,则不支持

My Register function in WebApiConfig is : WebApiConfig我的注册功能是:

public static void Register(HttpConfiguration config)
{
        config.MapHttpAttributeRoutes();
        config.Routes.MapHttpRoute("DefaultApi", "api/{namespace}/{controller}/{action}/{id}", new { id = UrlParameter.Optional });
}

My RegisterRoutes function is: 我的RegisterRoutes函数是:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    var r = routes.MapRoute(
                name: "Default",
                url: "v1/messages/{action}/{id}",
                defaults: new { id = UrlParameter.Optional },
                 namespaces: new[] { "BSB.Messages.Controllers.V1" }

            );
    r.DataTokens["Namespaces"] = new string[] { "BSB.Messages.Controllers.V1" };

    var r1 = routes.MapRoute(
               name: "V2",
               url: "v2/messages/{action}/{id}",
               defaults: new { id = UrlParameter.Optional },
               namespaces: new[] { "BSB.Messages.Controllers.V2" }
           );
    r1.DataTokens["Namespaces"] = new string[] { "BSB.Messages.Controllers.V2" };
}

I've called both functions from Global.asax 我已经从Global.asax调用了这两个函数

Can any one help me in this? 有人可以帮我吗? What I've missed here? 我在这里错过了什么?

Thanks, 谢谢,
Priya 普里亚

The second "RegisterRoutes" method applies only to MVC controllers not API controllers. 第二种“ RegisterRoutes”方法仅适用于MVC控制器,不适用于API控制器。 API routing should be done in the WebAPI startup. API路由应在WebAPI启动时完成。

The line: config.MapHttpAttributeRoutes(); 该行:config.MapHttpAttributeRoutes(); is going to work best for you, but will still require you to rename your controller classes. 最适合您,但是仍然需要重命名控制器类。 Take a look here for more on attribute routing: http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2 在此处查看有关属性路由的更多信息: http : //www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

Which explains that you could decorate your classes with attributes that define your routes: 这说明您可以使用定义路由的属性来修饰类:

namespace BSB.Messages.Controllers.V1
{
    [RoutePrefix("api/v1/messages")]    
    public class MessagesV1Controller : ApiController {...}
}

namespace BSB.Messages.Controllers.V2
{      
    [RoutePrefix("api/v2/messages")] 
    public class MessagesV2Controller : ApiController {...}
}

And in your WebApi startup you could either get rid of the MapHTTPRoute calls and go attribute only, or: 在您的WebApi启动中,您可以摆脱MapHTTPRoute调用并仅使用go属性,或者:

public static void Register(HttpConfiguration config)
{
    config.MapHttpAttributeRoutes();
    config.Routes.MapHttpRoute("DefaultApi", "api/v1/messages/{action}/{id}", new { controller = "MessagesV1", id = UrlParameter.Optional });
    config.Routes.MapHttpRoute("DefaultApi", "api/v2/messages/{action}/{id}", new { controller = "MessagesV2", id = UrlParameter.Optional });
    config.Routes.MapHttpRoute("DefaultApi", "api/{namespace}/{controller}/{action}/{id}", new { id = UrlParameter.Optional });
}

The above would result in the following working routes: 以上将导致以下工作路线:

Hope that helps! 希望有帮助!

Steve 史蒂夫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过名称空间限制Asp.net Web API / Owin对控制器的发现? - How to restrict Asp.net Web API / Owin discovery of controllers by namespace? Castle Windsor ASP.NET MVC 4和Web API相同的应用程序。 只有MVC控制器没有依赖关系? - Castle Windsor ASP.NET MVC 4 and Web API same application. Only if MVC controllers have no dependencies? ASP.NET Web API是否可以使用不同的控制器处理子资源 - Can ASP.NET Web API handle sub-resources with different controllers 具有相同控制器名称(不同名称空间)的ASP.net MVC单独解决方案 - ASP.net MVC seperate solutions with same controller name (different namespace) Asp.NET Web API中的控制器和类列表 - List Of Controllers And Classes In Asp.NET Web API 在ASP.NET Core Web API项目中保持控制器的温暖 - Keeping controllers warm in ASP.NET Core Web API project asp.net WebAPI两个具有相同路由但动词不同的控制器 - asp.net WebAPI Two controllers with same routes but different verbs 在ASP.NET 5中为Web API控制器模拟HttpContext - Mocking HttpContext for Web API Controllers in ASP.NET 5 ASP.NET Core Web API 中使用的命名空间应该是什么? - What should be the namespace used in ASP.NET Core Web API? 如何将ASP.NET MVC核心视图和ASP.NET WEB API控制器分离到单独的项目中? - How to seperate ASP.NET MVC core view and ASP.NET WEB API controllers into separate projects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM