[英]Different RoutePrefix, same controller name
I'm having a problem with splitting my web-api application into different areas (not mvc areas), using namespaces and RoutePrefix 我在使用命名空间和RoutePrefix将我的web-api应用程序拆分到不同的区域(而不是mvc区域)时遇到问题
The application is hosted using Owin Self Host, and in my Startup class I have the following. 该应用程序使用Owin Self Host托管,在我的Startup类中,我有以下内容。
HttpConfiguration config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
app.UseWebApi(config);
And my two controllers that I tested with 我测试过的两个控制器
[RoutePrefix("api/test")]
public class TestController : ApiController
{
[Route("")]
public IHttpActionResult Get()
{
return Ok("api");
}
}
[RoutePrefix("sync/test")]
public class TestController : ApiController
{
[Route("")]
public IHttpActionResult Get()
{
return Ok("sync");
}
}
These two controllers live in two different namespaces, Api and Sync. 这两个控制器存在两个不同的命名空间,Api和Sync。
When I try to access the two controllers with http://localhost/api/test and http://localhost/api/sync I get a 404. 当我尝试使用http:// localhost / api / test和http:// localhost / api / sync访问两个控制器时,我得到404。
But If I rename one of the controllers to eg TestApiController then both works. 但是,如果我将其中一个控制器重命名为例如TestApiController,那么两者都有效。
Someone having a good idea if it's possible to do what I want? 如果有可能做我想做的事,有人会有个好主意吗?
Unfortunately, Web API finds controllers by class name, ignoring the namespace. 遗憾的是,Web API按类名查找控制器,忽略命名空间。 This is a known issue and is not unique to attribute-based routing.
这是一个已知问题,并非基于属性的路由所独有。
The easiest work-around by far is to avoid the problem and use unique controller names. 迄今为止最简单的解决方法是避免问题并使用唯一的控制器名称。 But if you don't mind getting a little fancy, here's a solution:
但如果你不介意有点花哨,这是一个解决方案:
https://blogs.msdn.microsoft.com/webdev/2013/03/07/asp-net-web-api-using-namespaces-to-version-web-apis/ https://blogs.msdn.microsoft.com/webdev/2013/03/07/asp-net-web-api-using-namespaces-to-version-web-apis/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.