2017-05-08 67 views
2

我有一个OWIN的WebAPI 2应用程序。现在我试图添加一个MVC 5控制器到一切,但我的MVC路线没有被发现。我收到以下错误:WebAPI + MVC + Owin:MVC路线不起作用

No HTTP resource was found that matches the request URI ' https://localhost:44320/home '.

No type was found that matches the controller named 'home'.

控制器的名称是正确的(HomeController),它是公开的,我配置在Global.asax中路线:

protected void Application_Start() 
{ 
    HttpConfiguration config = GlobalConfiguration.Configuration; 
    ModelBinderConfig.RegisterModelBinders(config.Services); 
    GlobalConfiguration.Configure(WebApiConfig.Register); 
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
    RouteConfig.RegisterRoutes(RouteTable.Routes); 
    BundleConfig.RegisterBundles(BundleTable.Bundles); 
} 

我也有在OWIN启动类:

public class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     var config = GlobalConfiguration.Configuration; 

     var globalExceptionHandler = GetGlobalExceptionHandlerConfiguration(); 
     config.Services.Replace(typeof(IExceptionHandler), globalExceptionHandler); 
     app.UseWebApi(config); 
    } 
} 

我注意到,当我注释掉app.UseWebApi(config)线,MVC的路线开始重新工作。

有谁知道发生了什么事以及如何解决这个问题?

谢谢

+0

我相信你需要你的MVC路线后,注册你的WebAPI路线。这可以通过在'Application_Start'中完成,通过移动'GlobalConfiguration.Configure(WebApiConfig.Register);'在RouteConfig.RegisterRoutes(RouteTable.Routes)下面'' – peinearydevelopment

+0

@peinearydevelopment:我试过了,仍然得到相同的错误 – peflorencio

回答

2

app.UseWebApi()命令是自托管与OWIN,这与MVC不兼容,所以当你的项目是一个专用网络API项目,你只能用它。只要删除它,你就是金。

1

尝试将此代码添加到RouteConfig.cs文件中。

routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ); 
+0

感谢回答,但我已经做到了。如果我注释掉我提到的路线,路线就会起作用。所以路由配置正确。 – peflorencio

0

在我来说,我不得不从Application_Start移动所有配置后上面Startup.Configuration更换app.UseWebApi(config);