【问题标题】:AttributeRouting to Fix Two Params named IdAttributeRouting 修复两个名为 Id 的参数
【发布时间】:2018-12-28 06:36:57
【问题描述】:

所以我有一个项目使用默认的Route

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

我刚刚遇到了 7 年前在MVC Pro Tip: Don't use the "id" URL parameter in your routes 描述的情况。

他们拥有的解决方案非常棒,但此刻,我不想更改我的整个网站。我希望通过Attribute Routing 解决我的问题。

但是,我似乎无法让它工作,我收到了一个404 Error 页面。 (以防上面的链接失效,我会在这里详细描述代码)。

详情

在我的项目中,我使用ViewModelsViewModel(非常简单)定义为:

public class Foo {
    public int Id { get; set; }
    ...
}

我的BarController如下:

public ActionResult Create(string id) {
    if (string.IsNullOrWhiteSpace(id)) {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    ...
}

[HttpPost]
public ActionResult Create(string id, Foo viewModel) {
    if (string.IsNullOrWhiteSpace(id)) {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    ...
}

错误

当我导航到/Bar/Create/abc123 时,我可以很好地看到我的表单。但是,当我提交表单时,Model.IsValidfalse。查看Watch 窗口的this.ModelState 对象,我发现错误消息要说

值 'abc123' 对 Id 无效。'

我认为这是因为模型绑定器试图将 abc123 绑定到 ViewModel 上的 Id,该 int 作为 Id 属性。

我的尝试

到目前为止,这是我在 Controller 上尝试做的事情:

[Route("Bar/Create/{aid}", Name = "FooBarRouteName")]
public ActionResult Create(string aid) {
    if (string.IsNullOrWhiteSpace(aid)) {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    ...
}

[HttpPost]
public ActionResult Create(string aid, Foo viewModel) {
    if (string.IsNullOrWhiteSpace(aid)) {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    ...
}

现在的问题是当我导航到/Bar/Create/abc123 时,我得到一个404 Error 页面,甚至无法尝试提交表单。

谁能指出我正确的方向或找出我做错了什么?谢谢!

【问题讨论】:

  • 您能否考虑重命名Foo 中的id 属性?
  • 我是,但如果我做不到,我想知道如何完成这项工作。 =) 我喜欢学习如何解决这些问题,而不仅仅是“创可贴”
  • 您在 POST 操作中缺少一条路线。如果使用属性路由,则必须装饰所有操作
  • @Nkosi 谢谢。如果我最终走这条路线,我也会在那里添加它。

标签: c# asp.net-mvc asp.net-mvc-5 asp.net-mvc-routing attributerouting


【解决方案1】:

首先确保在基于约定的路由之前启用属性路由,以避免路由冲突。

//Attribute routing
routes.MapMvcAttributeRoutes();

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

您在 POST 操作中缺少路线。

如果使用属性路由,您必须装饰控制器上的所有操作

[HttpGet]
[Route("Bar/Create/{aid}", Name = "FooBarRouteName")] // GET Bar/Create/abc123
public ActionResult Create(string aid) {
    if (string.IsNullOrWhiteSpace(aid)) {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    ...
}

[HttpPost]
[Route("Bar/Create/{aid}")] // POST Bar/Create/abc123
public ActionResult Create(string aid, Foo viewModel) {
    if (string.IsNullOrWhiteSpace(aid)) {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    ...
}

【讨论】:

  • 仍然无法访问Bar/Create/abc123的初始页面
  • 谢谢!我已启用它,因为我在其他控制器上拥有它AttributeRouting。我也有HttpGetAttribute 。还是一样=/
  • 这行得通!我没有注意到ensure that attribute routing is enabled before convention-based routes
【解决方案2】:

转储问题:您是否更新了 RouteConfig 中的 RegisterRoutes?

routes.MapMvcAttributeRoutes(); // should be the first call

在注册默认路由之前,请确保您在上面调用。这是因为当路由器解析路由时,第一个配置首先匹配。

此外,您还需要确保 global.asax 中的顺序正确,以防您使用区域:

RouteConfig.RegisterRoutes(RouteTable.Routes); //needs to be first
AreaRegistration.RegisterAllAreas();

作为answered by Nkosi,您还应该将RouteAttribute 添加到您的其他操作中,并使用HttpGetAttribute 装饰您的GET 操作。

【讨论】:

  • 上面的电话是routes.MapRoute吗? @RoLYroLLs
  • 这是你的问题@RoLYroLLs
  • 我只是更仔细地注意到您的解决方案。试试看。
  • @RoLYroLLs 添加路线的顺序很重要。属性路由需要在基于约定的路由之前注册。
  • 哇!那行得通!我将测试我的其他路线以确保一切顺利。
猜你喜欢
  • 1970-01-01
  • 2015-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-10
  • 1970-01-01
  • 2011-12-11
  • 1970-01-01
相关资源
最近更新 更多