asp.net-mvc - WebAPI + MVC + Owin : MVC routes don't work

标签 asp.net-mvc asp.net-mvc-5 asp.net-web-api2 owin

我有一个带有 OWIN 的 WebAPI 2 应用程序。现在我正在尝试向所有内容添加一个 MVC 5 Controller ,但没有找到我的 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'.



Controller 的名称是正确的( 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 Startup 类(class):
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 路由又开始工作了。

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

谢谢

最佳答案

app.UseWebApi() 命令用于OWIN自托管,与MVC不兼容,所以只有在你的项目是专门的Web Api项目时才能使用。只需删除它,你就是金色的。

关于asp.net-mvc - WebAPI + MVC + Owin : MVC routes don't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43850125/

相关文章:

asp.net - 在 Web api IExceptionHandler 中获取请求正文

c# - Url.Link 在 Web Api 2 中抛出 Not Implemented 异常

Android 照片上传截断图像?

asp.net-mvc - MVC 5 : Asp.net Identity:如何对 UserRole 建模

.net - Swagger UI - 如何传递基于环境的 URI?

c# - 如何在多个 Controller 类之间重用 Action Method 验证

asp.net-mvc - 如何扩展 User.Identity 的可用属性

asp.net-mvc - 绑定(bind)排除 Asp.net MVC 不适用于 LINQ 实体

c# - 基于(强制转换)接口(interface)的 LINQ where 子句

javascript - 使用 JavaScript 和 jQuery 开发复杂网页的最佳实践