为什么友好 url 和 ASP.NET MVC5 路由属性每次都不起作用?
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
我想知道为什么友好 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