![](/img/trans.png)
[英]ASP.NET MVC 2 routing: Added language, doesn't work when left away in URL
[英]Why friendly url with ASP.NET MVC5 Routing attributes doesn't work everytime?
我想知道為什么從一個動作而不是另一個動作生成友好的URL。
讓我告訴你!
[RoutePrefix("fr-ca/guest")]
public class GuestController : BaseController
{
[Route("users/{id:int}/{ids:int}")]
public ActionResult Index(int id, int ids)
{
return View();
}
[Route("register/{id:int}/{ids:int}")]
public ActionResult Register(int id, int ids)
{
return View();
}
}
為了獲得友好的URL結果,我使用Razor像這樣:
@Html.ActionLink("some text", "Index", "Guest", new { id = 1, ids = 1 }, null)
@Html.ActionLink("some other text", "Register", "Guest", new { id = 1, ids = 1 }, null)
在路由配置文件中,我添加了以下行: routes.MapMvcAttributeRoutes();
因此,第一個正常工作,我得到了http:... / fr-ca / guest / users / 1/1,但是第二個無效! 我得到http:... / fr-ca / guest / register?id = 1&ids = 1
有人可以幫忙嗎?
謝謝!!
大衛
假設您沒有與此沖突的其他路線定義,
@Html.ActionLink("some other text", "Register", "Guest", new { id = 1, ids = 1 }, null)
將生成帶有href值的錨標記,其值為http://yoursitename/fr-ca/guest/register/1/1
,不是http:.../fr-ca/guest/register?id=1&ids=1
由於您已將模式指定為register/{id:int}/{ids:int}
因此它對於register/1/1
url應該可以正常工作,但對於/register?id=1&ids=2
無效。 那是預期的行為。
可以使用Html.RouteLink
代替使用Html.ActionLink
。 該幫助程序有一個重載,該重載使用要使用的路由的名稱。 您必須通過以下方式為路線添加名稱:
在您的控制器中:
[Route("register/{id:int}/{ids:int}", Name="GuestRegister")]
public ActionResult Register(int id, int ids)
{
return View();
}
您認為:
@Html.RouteLink("some other text", "GuestRegister", new { id = 1, ids = 1 }, null)
結果是:
http://.../fr-ca/guest/register/1/1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.