【问题标题】:All my routes are failing in my Web Api app我的所有路由在我的 Web Api 应用程序中都失败了
【发布时间】:2014-12-23 09:46:39
【问题描述】:

我创建了一个简单的 Web Api 应用程序,该应用程序在今晚早些时候可以运行。但是,在安装 Ninject 后,重新安装引用时遇到了一些问题(使用 Nuget 卸载 Ninject 时意外删除了一些)。

我认为这可能是由于我的 Startup.cs 文件:

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

        ConfigureOAuth(app);

        app.UseNinjectMiddleware(CreateKernel).UseNinjectWebApi(config);
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
    }

    public void ConfigureOAuth(IAppBuilder app)
    {
        OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            Provider = new SimpleAuthorizationServerProvider()
        };

        // Token Generation
        app.UseOAuthAuthorizationServer(OAuthServerOptions);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
    }

    private StandardKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Load(Assembly.GetExecutingAssembly());
        return kernel;
    }
}

这是我的控制器之一:

[RoutePrefix("api/Books")]

公共类 BooksController : ApiController { 私有 IBookService _bookService;

private BooksController(IBookService bookService)
{
    _bookService = bookService;
}

[Route("GetAllBooks")]
public IEnumerable<Book> GetAllBooks()
{
    return _bookService.GetAllBooks();
}

[Route("GetBook")]
public IHttpActionResult GetBook(string isbn)
{
    var book = _bookService.GetBook(isbn);

    if (book == null)
        return NotFound();

    return Ok(book);
}

但是,我对这些路由中的任何一个进行的每次调用都会返回 404,例如:

http://localhost/api/Books/GetAllBooks

如您所见,我将 Ninject 与 Owin 一起使用,所以我不确定这是否会导致问题。

有人看过这个或者有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

    我看到您正在使用属性路由。为了让它工作,你需要打电话给config.MapHttpAttributeRoutes();

    HttpConfiguration config = new HttpConfiguration();
    config.MapHttpAttributeRoutes();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-27
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多