Asp.net mvc ASP.NET MVC:如何传递查询字符串变量
我已经在web表单上工作了一段时间,不习惯mvc 以下是我想做的:Asp.net mvc ASP.NET MVC:如何传递查询字符串变量,asp.net-mvc,query-string,Asp.net Mvc,Query String,我已经在web表单上工作了一段时间,不习惯mvc 以下是我想做的: <% if guest then %> URL.RouteURL("AcccountCreate?login=guest") <% end if %> URL.RouteURL(“AcccountCreate?login=guest”) 但是,我不能这样做,因为它说没有创建具有此名称的路由。我认为为这个创建路由是愚蠢的。 <%= Url.Action("AcccountCreate",
<% if guest then %>
URL.RouteURL("AcccountCreate?login=guest")
<% end if %>
URL.RouteURL(“AcccountCreate?login=guest”)
但是,我不能这样做,因为它说没有创建具有此名称的路由。我认为为这个创建路由是愚蠢的。
<%= Url.Action("AcccountCreate", new { login = "guest" }) %>
或者,如果要直接生成链接:
<%= Html.ActionLink("create new account", "AcccountCreate", new { login = "guest" }) %>
您还可以指定控制器:
<%= Html.ActionLink(
"create new account", // text of the link
"Acccount", // controller name
"Create", // action name
new { login = "guest" } // route values (if not part of the route will be sent as query string)
) %>
现在还不是MVC路由的真正目的。最好将您的URL设置为:
AccountCreate/guest
然后让您的操作接受该参数
public ActionResult AccountCreate(string AccountName)
{
//AccountName == guest
return View();
}
然后您可以有一个路由条目,如:
routes.MapRoute(
"AccountCreate", // Route name
"{controller}/{action}/{AccountName}", // URL with parameters
new { controller = "AccountCreate", action = "Index", AccountName = UrlParameter.Optional } // Parameter defaults
);
但是,如果您创建一个带有参数且没有匹配路由的
actionlink
,它将为您创建一个querystring
变量。是的,正如Darin所说(+1)。你得到的错误是user455100,它告诉你,你正在使用的特定重载需要你经过的路线的名称,大概是你的URL。太棒了,太棒了,thnx