没有收到get参数

Not receiving get parameters

我正在使用 ASP.NET MVC 5

我在路由和参数方面都遇到了问题。

我的 ControllerBase

中有这个功能
[HttpGet]
[Route("~/obtenerAngulos/{Conex_AT}/{Conex_BT}")]
public JsonResult obtenerAngulos(string Conex_AT, string Conex_BT)
{
    return Json(
        new
        {
            AT = Conex_AT,
            BT = Conex_BT
        }
        , JsonRequestBehavior.AllowGet);
}

而且我开始在接收第二个参数时遇到问题 Conex_BT Url.Action() returns 这个路由 http://localhost:53645/Base/obtenerAngulos?Conex_AT=Y&Conex_BT=y 问题是 Conex_BT 总是空的

然后我尝试使用路线并为其添加数据注释 [Route("~/obtenerAngulos/{Conex_AT}/{Conex_BT}")] 但是 Url.Action() 我一直得到与以前相同的路线。

即使我尝试像 http://localhost:53645/Base/obtenerAngulos/AA/BB 那样手动编写它,我也会得到

HTTP Error 404.0 - Not Found

我提到这两个问题是因为我很确定它们是相关的。

这里是路由配置

RouteConfig.cs

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

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


}

确保您已在路由集合上启用属性路由。

//enable attribute routes
routes.MapMvcAttributeRoutes(); 

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

这意味着下面应该匹配 obtenerAngulos/y/x

public class  ControllerBase: Controller {
    //Matches obtenerAngulos/y/x
    [HttpGet]
    [Route("~/obtenerAngulos/{Conex_AT}/{Conex_BT}")]
    public JsonResult obtenerAngulos(string Conex_AT, string Conex_BT) {
        //...
    }
}

如果需要,方法属性上的波浪号 (~) 用于覆盖任何路由前缀。

路由在路由 table 中的匹配顺序与它们添加的顺序相同。在您的示例中,您在属性路由之前注册了基于约定的路由。一旦路由匹配,它就不再寻找其他匹配项。

引用Attribute Routing in ASP.NET MVC 5