[英]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.