C# ASP MVC 5属性路由未注册路由 概述

C# ASP MVC 5属性路由未注册路由 概述,c#,asp.net-mvc-5,attributerouting,C#,Asp.net Mvc 5,Attributerouting,我目前正在尝试让属性路由与我的api控制器一起工作。它似乎不起作用,我不确定为什么。我不确定我是否错过了关键的一步,或者问题可能是什么 问题 我正试图点击localhost/api/user/custom?test=1,但我得到了404(我希望这能起作用) 如果我点击localhost/api/customapi?test=1,我将成功进入我的方法 为什么第一个url不起作用 安装程序 我的设置如下: CustomController.cs [System.Web.Mvc.RoutePrefix

我目前正在尝试让属性路由与我的api控制器一起工作。它似乎不起作用,我不确定为什么。我不确定我是否错过了关键的一步,或者问题可能是什么

问题 我正试图点击localhost/api/user/custom?test=1,但我得到了404(我希望这能起作用)

如果我点击localhost/api/customapi?test=1,我将成功进入我的方法

为什么第一个url不起作用

安装程序 我的设置如下:

CustomController.cs

[System.Web.Mvc.RoutePrefix("api")]
public class CustomApiController : ApiController
{
    [System.Web.Mvc.Route("user/custom")]
    [System.Web.Http.HttpGet]
    public async Task<CustomResponse> Get([FromUri] CustomRequest request)
    {
        //Work
    }
}
public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        ...(json serializer settings)...

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
            );
    }
}
public static class RouteConfig
{
    public static void Register(RouteCollection routes, bool registerAreas = true)
    {
        if(registerAreas)
        {
            AreaRegistration.RegisterAllAreas();
        }

        //Ignore Routes
        ...

        //Register specific routes
        routes.MapRoute("HomeUrl", "home", new { controller = "Home", action = "Index" });
        .
        .

        routes.MapRoute(
            "Default", //Route name
            "{controller}/{action}/{id}",  //URL with parameters
            new { controller = "Home", action = "Index", id =UrlParameter.Optional }
            );
    }
}    
public class Global : HttpApplication
{
    protected void Application_Start()
    {
        ....(app startup stuff)...

        GlobalConfiguration.Configure(WebApiConfig.Register);
        BundleConfig.Register(BundleTable.Bundles);

        ....(more app startup stuff)...

        RouteConfig.Register(RouteTable.Routes);
    }
}
RouteConfig.cs

[System.Web.Mvc.RoutePrefix("api")]
public class CustomApiController : ApiController
{
    [System.Web.Mvc.Route("user/custom")]
    [System.Web.Http.HttpGet]
    public async Task<CustomResponse> Get([FromUri] CustomRequest request)
    {
        //Work
    }
}
public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        ...(json serializer settings)...

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
            );
    }
}
public static class RouteConfig
{
    public static void Register(RouteCollection routes, bool registerAreas = true)
    {
        if(registerAreas)
        {
            AreaRegistration.RegisterAllAreas();
        }

        //Ignore Routes
        ...

        //Register specific routes
        routes.MapRoute("HomeUrl", "home", new { controller = "Home", action = "Index" });
        .
        .

        routes.MapRoute(
            "Default", //Route name
            "{controller}/{action}/{id}",  //URL with parameters
            new { controller = "Home", action = "Index", id =UrlParameter.Optional }
            );
    }
}    
public class Global : HttpApplication
{
    protected void Application_Start()
    {
        ....(app startup stuff)...

        GlobalConfiguration.Configure(WebApiConfig.Register);
        BundleConfig.Register(BundleTable.Bundles);

        ....(more app startup stuff)...

        RouteConfig.Register(RouteTable.Routes);
    }
}
Global.asax.cs

[System.Web.Mvc.RoutePrefix("api")]
public class CustomApiController : ApiController
{
    [System.Web.Mvc.Route("user/custom")]
    [System.Web.Http.HttpGet]
    public async Task<CustomResponse> Get([FromUri] CustomRequest request)
    {
        //Work
    }
}
public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        ...(json serializer settings)...

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
            );
    }
}
public static class RouteConfig
{
    public static void Register(RouteCollection routes, bool registerAreas = true)
    {
        if(registerAreas)
        {
            AreaRegistration.RegisterAllAreas();
        }

        //Ignore Routes
        ...

        //Register specific routes
        routes.MapRoute("HomeUrl", "home", new { controller = "Home", action = "Index" });
        .
        .

        routes.MapRoute(
            "Default", //Route name
            "{controller}/{action}/{id}",  //URL with parameters
            new { controller = "Home", action = "Index", id =UrlParameter.Optional }
            );
    }
}    
public class Global : HttpApplication
{
    protected void Application_Start()
    {
        ....(app startup stuff)...

        GlobalConfiguration.Configure(WebApiConfig.Register);
        BundleConfig.Register(BundleTable.Bundles);

        ....(more app startup stuff)...

        RouteConfig.Register(RouteTable.Routes);
    }
}

我在路由中使用了错误的名称空间

[System.Web.Http.Route("")]   //Use this namespace for Web API 2
[System.Web.Mvc.Route("")]

[System.Web.Http.RoutePrefix("api")]  //Use this namespace Web API 2
[System.Web.Mvc.RoutePrefix("api")]

谢谢你——我是在一个MVC路由问题弄得我毛骨悚然之后来到这里的(结果和你的一样,但情况正好相反)。我不明白为什么MVC站点中的一个控制器没有被路由选择。最终我发现它使用的是Http.Route而不是Mvc.Route。再次感谢,搞定了。谢谢