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

时间:2010-10-01 16:17:06

标签: asp.net-mvc query-string

我已经在网络表单上工作了一段时间,并不习惯mvc。

这是我想要做的:

<% if guest then %>
    URL.RouteURL("AcccountCreate?login=guest")
<% end if %>

但是我不能这样做,因为它说没有创建具有此名称的路由。而且我认为为此创建一条路线是愚蠢的。

2 个答案:

答案 0 :(得分:3)

<%= 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)
) %>

答案 1 :(得分:1)

现在并非真正使用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变量。

相关问题