[英]ASP.NET MVC: How can I pass a querystring variable
I've worked on web forms for a while and am not used to mvc. 我已经在Web表单上工作了一段时间,并且不习惯使用mvc。
Here is what i want to do: 这是我想做的:
<% if guest then %>
URL.RouteURL("AcccountCreate?login=guest")
<% end if %>
However I can't do this because it says a route with this name has not been created. 但是我不能这样做,因为它说尚未创建具有该名称的路由。 And I am assuming it would be silly to create a route for this.
我认为为此创建一条路线将很愚蠢。
<%= Url.Action("AcccountCreate", new { login = "guest" }) %>
or if you want to generate directly a link: 或者,如果您想直接生成链接:
<%= Html.ActionLink("create new account", "AcccountCreate", new { login = "guest" }) %>
You can also specify the controller: 您还可以指定控制器:
<%= 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)
) %>
That is not really now MVC routing was intended to be used. 现在并不是真的打算使用MVC路由。 Better to set your URL like:
最好将您的网址设置为:
AccountCreate/guest
And then have your action accept that parameter 然后让您的操作接受该参数
public ActionResult AccountCreate(string AccountName)
{
//AccountName == guest
return View();
}
Then you could have a routing entry like: 然后,您可能会有一个路由条目,例如:
routes.MapRoute(
"AccountCreate", // Route name
"{controller}/{action}/{AccountName}", // URL with parameters
new { controller = "AccountCreate", action = "Index", AccountName = UrlParameter.Optional } // Parameter defaults
);
However, with that if you create an actionlink
with a parameter and no matching route it will create a querystring
variable for you. 然而,如果你创建一个
actionlink
一个参数,并没有匹配的路由,将创建一个querystring
变量为您服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.