2014-12-23 46 views
1

我創建了一個簡單的Web Api應用程序,它在今天早些時候工作。然而,在安裝Ninject之後,重新安裝引用時遇到了一些問題(在用Nuget卸載Ninject時意外刪除了一些內容)。我所有的路線都在我的Web Api應用程序中失敗

我想它可能是由於我的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所以我不知道這是導致這個問題。

之前有人看過這個或有什麼想法嗎?

回答

1

我看到你正在使用屬性路由。爲了它的工作,你需要撥打config.MapHttpAttributeRoutes();

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

這解決了它。很好,謝謝! – alimac83

相關問題