【问题标题】:ASP.NET WebAPI controller not working even though routings are correct即使路由正确,ASP.NET WebAPI 控制器也无法正常工作
【发布时间】:2015-10-02 08:38:17
【问题描述】:

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

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")
        };
    }
}

如您所见,它非常简单。可能有什么问题?

【问题讨论】:

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


    【解决方案1】:

    我意识到我的控制器类是隐含的internal,因为我没有另外指定。必须是 public 才能让 Owin 通过反射找到类。因此它与路由无关......

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 2018-06-23
      • 1970-01-01
      • 2018-04-14
      相关资源
      最近更新 更多