简体   繁体   English

不同的RoutePrefix,相同的控制器名称

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

相关问题 与控制器名称不同的RoutePrefix不起作用 - RoutePrefix different than Controller name doesn't work 将表单从模式局部视图发布到具有routeprefix属性的其他控制器 - Post form from modal partial view to a different controller with a routeprefix attribute 在控制器上未找到带有空RoutePrefix的路由 - Route is not found with empty RoutePrefix on controller WebApi中具有相同参数类型和不同[Route(“ / cars”)] / [RoutePrefix(“ api / v1 / data”)]的多个Post请求 - Multiple Post requests in WebApi with same parameter types and different [Route(“/cars”)]/[RoutePrefix(“api/v1/data”)] 创建具有相同名称但路径前缀不同的其他控制器 - Creating different controller with same name but different route Prefix 在基本控制器上使用WebApi RoutePrefix属性 - Using WebApi RoutePrefix attribute on a base controller .Net MVC控制器使用相同的cshtml作为视图使用不同的路由名称 - .Net MVC Controller different route name using the same cshtml as view ASP MVC Controller发布具有不同值的相同html输入名称 - ASP MVC Controller post same html input name with different values 我可以通过Ajax在不同位置访问同名控制器吗? - Can I access a controller with same name in a different location via ajax? 如何在API控制器中有两个名称相同但参数不同的方法? - How to have two methods in API Controller with same name but different arguments?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM