繁体   English   中英

ASP.NET MVC:如何传递查询字符串变量

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM