Issue when using IAppBuilder.UseWebApi

最后都变了- 提交于 2019-12-24 08:32:51

问题


I am creating a SPA (Singe Page Application) using WebApis in ASP .NET MVC5. For WebAPis I am using attribute routing. For requests other than Web API, I have configured a Catch-All route that maps to Home controller and Index action Here is my code

I have the following configuration code in my StartUp.cs file

using System.Web.Routing;

[assembly: OwinStartup(typeof(MyApp.Startup))]

namespace MyApp

{

public class Startup
{

    public void Configuration(IAppBuilder app)
    {

        GlobalConfiguration.Configure(WebApiConfig.Register);
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);            


        //app.UseWebApi(GlobalConfiguration.Configuration); 

    }

}
}

My WebApiConfig.Register method is as follows

public static class WebApiConfig
    {
    public static void Register(HttpConfiguration config)
    {

        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

    }
}

For routes other than WebApis, I have the following route registered

 public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "CatchAll",
            url: "{*url}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

    }
}

Now in the Configuration method in the StartUp class, if I uncomment this line

        app.UseWebApi(GlobalConfiguration.Configuration)

and try to access any route, I get this error

<Error>
    <Message>
    No HTTP resource was found that matches the request URI 'http://localhost:17212/orders'.
    </Message>
    <MessageDetail>
    No type was found that matches the controller named 'Home'.
    </MessageDetail>
</Error>

Why does this happen ? Is it OK to keep this line commented ?


回答1:


You are mixing configurations. app.UseWebApi is primarily for OWIN pipline not MVC, so when you use that setup is negates your MVC configuration as it takes over the inspection of incoming requests. If you are mixing both MVC and Web API you can safely keep that line commented.



来源:https://stackoverflow.com/questions/39746625/issue-when-using-iappbuilder-usewebapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!