![](/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.