【问题标题】:ActionLink builds wrong URL when using System.Web.Mvc.RouteAttribute使用 System.Web.Mvc.RouteAttribute 时 ActionLink 构建错误的 URL
【发布时间】:2018-11-04 22:23:18
【问题描述】:

我正在使用 MVC 5,我正在尝试通过 System.Web.Mvc.RouteAttribute 完成路由。

因此,大多数操作都有效,而一个则无效。

我创建了一个Delete-action 和一个Edit-action。两者看起来一样。

这里是Delete-方法:

[Route("data/links/delete/{id}")]
public async Task<ActionResult> Delete(int? id)
{
    // ....
    return View(link);
}

// in the view, DeleteConfirmed is called on submit

[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
[Route("data/links/delete/{id}")]
public async Task<ActionResult> DeleteConfirmed(int id)
{
    // ....
    return RedirectToAction("Index");
}

这里是Edit-方法:

[Route("data/links/edit/{id}")]
public async Task<ActionResult> Edit(int? id)
{
    // ....
    return View(link);
}

[HttpPost]
[ValidateAntiForgeryToken]
[Route("data/links/edit")]
public async Task<ActionResult> Edit([Bind(Include = "Id,LinkText,Url,Image,Description")] Link link)
{
    // ....
    return this.RedirectToAction("Index");
}

所以,它们的路由看起来是一样的。 我称之为GET-methods 的链接也一样:

@Html.ActionLink("Bearbeiten", "Edit", new { id = item.Id })
@Html.ActionLink("Löschen", "Delete", new { id = item.Id })

有趣的是:

the link to edit gets rendered: http://localhost:45132/data/links/edit?id=2
the link to delete gets rendered: http://localhost:45132/data/links/delete/2

为什么编辑会渲染到edit?id=2,而删除会渲染到delete/2

编辑链接不起作用。当我在http://localhost:45132/data/links/edit/2 手动输入编辑页面时,链接有效。 但是 ActionLink 给我一个错误的 URL。有什么想法吗?

更新

我的 RouteConfig.cs

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

    routes.MapMvcAttributeRoutes();

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

【问题讨论】:

  • RouteConfig.cs的内容是什么?
  • @SeM 添加了 RouteConfig.cs
  • @MatthiasBurger 是的,没仔细看。也许它正在从 post 方法中获取路线。

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


【解决方案1】:

我建议您改用 RouteLink helper method

Name 属性定义为您的Route 属性,例如:

[Route("data/links/edit/{id}", Name = "MyRoute")]

然后使用:

@Html.RouteLink("Bearbeiten", "MyRoute", new { id = item.Id })

【讨论】:

    【解决方案2】:

    它的行为符合设计。您在编辑中有一个复杂的模型,它也不是路径模板的一部分。所以它必须将生成链接时提供的属性放在查询字符串中。

    要获得所需的路线...

    [HttpGet]
    [Route("data/links/edit/{id:int}")] // GET data/links/edit/2
    public async Task<ActionResult> Edit(int id) {
        // ....
        return View(link);
    }
    
    [HttpPost]
    [ValidateAntiForgeryToken]
    [Route("data/links/edit/{id:int}")] // POST data/links/edit/2
    public async Task<ActionResult> Edit(int id, [Bind(Include = "Id,LinkText,Url,Image,Description")] Link link) {
        // ....
        return this.RedirectToAction("Index");
    }
    

    【讨论】:

    • 啊,我需要在调用 GET 的 POST 中添加 id 作为参数...?!我明白了……我坚持使用 SeMs 的答案。对我来说看起来更干净
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    • 2012-04-20
    相关资源
    最近更新 更多