简体   繁体   English

Web Api Controller在其他项目中,路由属性不起作用

[英]Web Api Controller in other project, route attribute not working

I have a solution with two projects. 我有两个项目的解决方案。 One Web Api bootstap project and the other is a class library. 一个Web Api bootstap项目,另一个是类库。

The class library contains a ApiController with attribute routing. 类库包含一个带属性路由的ApiController。 I add a reference from web api project to the class library and expect this to just work. 我将web api项目中的引用添加到类库中,并希望它能够正常工作。

The routing in the web api is configured: Web api中的路由配置为:

config.MapHttpAttributeRoutes();

The controller is simple and looks like: 控制器很简单,看起来像:

public class AlertApiController:ApiController
{
    [Route("alert")]
    [HttpGet]
    public HttpResponseMessage GetAlert()
    {
        return Request.CreateResponse<string>(HttpStatusCode.OK,  "alert");
    }
}

But I get a 404 when going to the url "/alert". 但是当我转到网址“/ alert”时我得到了404。

What am I missing here? 我在这里错过了什么? Why can't I use this controller? 为什么我不能使用这个控制器? The assembly is definitely loaded so I don't think http://www.strathweb.com/2012/06/using-controllers-from-an-external-assembly-in-asp-net-web-api/ is the answer here. 组装肯定是加载所以我不认为http://www.strathweb.com/2012/06/using-controllers-from-an-external-assembly-in-asp-net-web-api/是答案这里。

Any ideas? 有任何想法吗?

Try this. 试试这个。 Create a class in your class library project that looks like this, 在类库项目中创建一个如下所示的类,

public static class MyApiConfig {


  public static void Register(HttpConfiguration config) {
      config.MapHttpAttributeRoutes();
  }
}

And wherever you are currently calling the config.MapHttpAttributeRoutes() , instead call MyApiConfig.Register(config) . 无论你当前在哪里调用config.MapHttpAttributeRoutes() ,而是调用MyApiConfig.Register(config)

One possibility is you have 2 routes on different controllers with the same name. 一种可能性是您在具有相同名称的不同控制器上有2条路由。

I had 2 controllers both named "UploadController", each in a different namespace and each decorated with a different [RoutePrefix()]. 我有两个名为“UploadController”的控制器,每个控制器位于不同的命名空间中,每个控制器都用不同的[RoutePrefix()]装饰。 When I tried to access either route I got a 404. 当我试图访问任一路线时,我得到了404。

It started working when I changed the name of one of the controllers. 当我更改其中一个控制器的名称时,它开始工作。 It seems the Route Attribute is only keyed on the class name and ignores the namespace. 似乎Route属性仅键入类名并忽略命名空间。

We were trying to solve a similar problem. 我们试图解决类似的问题。 The routes within the external assembly were not registering correctly. 外部程序集中的路径未正确注册。 We found one additional detail when trying the solution shown on this page. 我们在尝试此页面上显示的解决方案时发现了一个额外的细节。 The call to the external assembly "MyApiConfig.Register" needed to come before the call to MapHttpRoute 在调用MapHttpRoute之前需要调用外部程序集“MyApiConfig.Register”

HttpConfiguration config = new HttpConfiguration();
MyExternalNamespace.MyApiConfig.Register(config); //This needs to be before the call to "config.Routes.MapHttpRoute(..."
config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM