C# 具有重复使用查询参数的ASP.NET MVC问题
我有以下几条路线:C# 具有重复使用查询参数的ASP.NET MVC问题,c#,asp.net-mvc,C#,Asp.net Mvc,我有以下几条路线: routes.MapRoute( "Default", "user/{userId}/{controller}/{action}", new {controller = "Home", action = "Index" } ); routes.MapRoute( "Short",
routes.MapRoute(
"Default",
"user/{userId}/{controller}/{action}",
new {controller = "Home", action = "Index" }
);
routes.MapRoute(
"Short",
"{controller}/{action}",
new { controller = "Home", action = "Index"}
);
我在浏览器中的当前位置:
此页面上有以下链接:
@Html.ActionLink("Friends", "Index", "Friends")
@Html.ActionLink("Information", "Index", "UserInfo", new { userId = (string)null },null)
MVC重用查询参数,因此第一个生成的链接:
我的网站/用户/197/朋友
生成第二个链接:
我的网站/UserInfo?userId=197
为什么第二个链接中的userId的值为197?为什么没有链接:
我的网站/用户信息
我可能会这样做
http://foo.com/user/events/197
http://foo.com/user/events?userId=197
我发现,我越是尝试使用ASP.NET路由约定,开发应用程序的时间就越长
public class UserController : Controller
{
public ActionResult Events(long userId)
{
//Do Something...
}
}
public class FriendsController : Controller
{
public ActionResult Index(long userId)
{
//Do Something...
}
}
我可能会这样做
http://foo.com/user/events/197
http://foo.com/user/events?userId=197
我发现,我越是尝试使用ASP.NET路由约定,开发应用程序的时间就越长
public class UserController : Controller
{
public ActionResult Events(long userId)
{
//Do Something...
}
}
public class FriendsController : Controller
{
public ActionResult Index(long userId)
{
//Do Something...
}
}
我无法确定MVC决定重用手头上的任何路由值的确切位置,但以下是我在项目中使用的内容:
//
// This fixes "ambient values" problem:
// http://stackoverflow.com/questions/2651675/asp-net-mvc-html-actionlink-maintains-route-values
// http://stackoverflow.com/questions/780643/asp-net-mvc-html-actionlink-keeping-route-value-i-dont-want
return new UrlHelper(
new RequestContext(
HttpContext.Current,
new RouteData {
Route = urlHelper.RequestContext.RouteData.Route,
RouteHandler = urlHelper.RequestContext.RouteData.RouteHandler
}), urlHelper.RouteCollection)
.Action(actionName, controllerName, routeValuesWithArea);
这里的关键是,无论是
RouteData.DataTokens
还是RouteData.Values
都没有设置,因此MVC都不可能重用任何东西。我无法确定MVC决定重用手头任何路由值的确切位置,但以下是我在项目中使用的:
//
// This fixes "ambient values" problem:
// http://stackoverflow.com/questions/2651675/asp-net-mvc-html-actionlink-maintains-route-values
// http://stackoverflow.com/questions/780643/asp-net-mvc-html-actionlink-keeping-route-value-i-dont-want
return new UrlHelper(
new RequestContext(
HttpContext.Current,
new RouteData {
Route = urlHelper.RequestContext.RouteData.Route,
RouteHandler = urlHelper.RequestContext.RouteData.RouteHandler
}), urlHelper.RouteCollection)
.Action(actionName, controllerName, routeValuesWithArea);
这里的关键是,无论是
RouteData.DataTokens
还是RouteData.Values
都没有设置,因此MVC不可能重用任何东西。我想你不理解这个问题我想你不理解这个问题我可以看看它是如何用来生成链接的吗?我可以看看它是如何用来生成链接的吗?