[英]ASP.NET MVC: How can I pass a querystring variable
我已經在Web表單上工作了一段時間,並且不習慣使用mvc。
這是我想做的:
<% if guest then %>
URL.RouteURL("AcccountCreate?login=guest")
<% end if %>
但是我不能這樣做,因為它說尚未創建具有該名稱的路由。 我認為為此創建一條路線將很愚蠢。
<%= 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路由。 最好將您的網址設置為:
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
變量為您服務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.