Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法命中MVC项目中的Web API 2控制器_C#_Asp.net Web Api_Asp.net Web Api2_Asp.net Web Api Routing - Fatal编程技术网

C# 无法命中MVC项目中的Web API 2控制器

C# 无法命中MVC项目中的Web API 2控制器,c#,asp.net-web-api,asp.net-web-api2,asp.net-web-api-routing,C#,Asp.net Web Api,Asp.net Web Api2,Asp.net Web Api Routing,我无法访问使用ASP.NETWebAPI2控制器在MVC项目中编写的API localhost:57323/api/billpayment/getbillers/10工作正常,但localhost:57323/api/billpayment/getbillers不工作 WebAPI.config public static class WebApiConfig { public static void Register(HttpConfiguration config) {

我无法访问使用ASP.NETWebAPI2控制器在MVC项目中编写的API

localhost:57323/api/billpayment/getbillers/10
工作正常,但
localhost:57323/api/billpayment/getbillers
不工作

WebAPI.config

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 }
        );
    }
}
Global.asax

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
}

由于路由的配置方式,您遇到了路由冲突

在标准MVC路由之前,需要将Web API路由添加到路由表中

更新

public class MvcApplication : System.Web.HttpApplication {
    protected void Application_Start() {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register); //<-- This MUST come before
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes); //<-- THIS to avoid conflicts
        BundleConfig.RegisterBundles(BundleTable.Bundles);        
    }
}
公共类MVCAPApplication:System.Web.HttpApplication{
受保护的无效应用程序\u Start(){
RegisterAllAreas();

GlobalConfiguration.Configure(WebApiConfig.Register);//By
不起作用
你是什么意思?你得到404?500?@trailmax得到404你还有其他
GetBillers
方法吗?@Liam没有其他GetBillers方法尝试从global.asax隐藏
RouteConfig.registerOutes(RouteTable.Routes);
,然后测试一次