即使路由正确,ASP.NET WebAPI控制器也无法正常工作

时间:2015-10-02 08:38:17

标签: c# asp.net-web-api owin

我的API控制器永远不会被实例化,即使我已根据thisthis问题多次确定我的路由应该是正确的。

public void Configuration(IAppBuilder app)
{
    var config = new HttpConfiguration();

    config.Routes.MapHttpRoute("DefaultApi",
         routeTemplate: "api/{controller}/{id}",
         defaults: new { id = RouteParameter.Optional }
     );

     app.UseWebApi(config);
}

这就是我的控制器的样子:

class RaspberryController : ApiController
{
    public HttpResponseMessage Get()
    {
        return new HttpResponseMessage
        {
            Content = new StringContent("Test from Owin")
        };
    }
}

你可以看到它非常简单。什么可能是错的?

1 个答案:

答案 0 :(得分:0)

I realized that my controller class were implicitly internal since I didnt specify otherwise. It must be public in order for Owin to find the class by reflection. Thus it had nothing to do with routing...

public class RaspberryController : ApiController
{
    public HttpResponseMessage Get()
    {
        return new HttpResponseMessage
        {
            Content = new StringContent("Test from Owin")
        };
    }
}
相关问题