Url.Action地图从路线属性错误的链接 [英] Url.Action map a wrong link from Route attribute

查看:123
本文介绍了Url.Action地图从路线属性错误的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是目标控制器和行动:

This is target controller and action:

[RoutePrefix("Editor")]
public class EditorController : Controller

[HttpGet]
    [Route("{id:int}")]
    public ActionResult Edit(int id)

地图调用方法:

@Url.Action("Edit", "Editor", new { id = page.Id})

结果:
/编辑?ID = 1

result: /Editor?id=1

所需的结果:
/编辑/ 1

required result: /Editor/1

推荐答案

要实现你想要的,你必须使用一个路由名称对应的结果是:

To achieve the result you want you have to use a route name:

[HttpGet]
[Route("{id:int}", Name = "EditorById")]
public ActionResult Edit(int id)

然后在你的观点,你会用Url.RouteUrl代替Url.Action:

Then in your view you would use Url.RouteUrl instead of Url.Action:

@Url.RouteUrl("EditorById", new { controller = "Editor", Id = 1, action = "Edit" })

希望这有助于

这篇关于Url.Action地图从路线属性错误的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆