WebAPI属性路由在OWIN中不起作用

时间:2016-11-25 08:16:51

标签: asp.net asp.net-web-api asp.net-web-api2 owin

我有一个问题,WebApi为我的路线抛出404错误。控制器的一个例子是:

[System.Web.Http.RoutePrefix("test/categories")]
public class CategoriesController : ControllerBase
{

    [System.Web.Http.Route("hello")]
    [HttpGet]
    [AcceptVerbs("GET")]
    public IHttpActionResult Get([FromUri] QueryOptions options)
    {
        // Usual controller stuff happens here
    }

    [System.Web.Http.Route("{id:int}")]
    [HttpGet]
    [AcceptVerbs("GET")]
    public IHttpActionResult GetById(int id)
    {
        // Usual controller stuff happens here
    }
}

https://api.testdomain.com/test/categories/hellohttps://api.testdomain.com/test/categories/1的GET请求都会抛出404 Not Found错误。该应用程序在IIS(而不是IIS Express)上运行。 404是IIS 404,表示应用程序正在启动,但路由失败。我在startup.cs中附加了断点,它肯定被击中了。

我的startup.cs文件包含:

    public void Configuration(IAppBuilder app)
    {            
        var config = new HttpConfiguration
        {
            IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always,
            DependencyResolver = new SimpleInjectorWebApiDependencyResolver(_container) // _container is a SimpleInjector IoC container; shouldn't affect things.
        };

        WebApiConfig.Register(config);

        app.UseCors(CorsOptions.AllowAll);
        app.UseWebApi(config); // Have tried commenting this out with no luck
    }

我的global.asax包含:

    protected void Application_Start(object sender, EventArgs e)
    {
        GlobalConfiguration.Configure(WebApiConfig.Register); // Have also tried commenting this out with no luck
    }

WebApiConfig.cs看起来像这样:

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

        config.Formatters.Add(new FilePropertyInfoMediaTypeFormatter());

    }
}

我安装了以下软件包:

<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Cors" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.HelpPage" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net452" />

我不知道为什么路由不起作用。它是从其他项目移植的代码,方法(即startup.cs中的代码等)与工作项目相同。

我围成一圈,所以任何帮助都会感激不尽。

1 个答案:

答案 0 :(得分:1)

您正在使用IIS和OWIN混合HttpConfiguration对象的配置。它是一个或另一个。使用两者都会导致冲突。

<强> WebApiConfig.cs

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

        config.Formatters.Add(new FilePropertyInfoMediaTypeFormatter());

        config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

        config.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(_container);
    }
}

<强> Startup.cs

public class Startup {
    public void Configuration(IAppBuilder app) {  
        app.UseCors(CorsOptions.AllowAll);
    }
}

Global.asax保持不变

protected void Application_Start(object sender, EventArgs e) {
    GlobalConfiguration.Configure(WebApiConfig.Register);
}