我是mvc
的新用户。我正在创建一个测试应用程序mvc
。为什么我必须将参数作为查询字符串传递给MVC中的Action方法?
here我已经研究了mvc
作品以URL为/[Controller]/[ActionName]/[Parameters]
但在我的应用程序必须通过参数作为/home/index?name=test
。我认为它应该作为/home/index/test
。但是这样做不起作用。
以下是在Global.asax.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
ActionMethod
在homeController
public ActionResult Index(String name)
{
ViewBag.name = name;
return View();
}
路由代码Index.cshtml
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@ViewBag.name</h2>
任何人都可以帮我找出为什么它不工作在/home/index/test
格式。
谢谢。
什么是你所得到的错误?索引操作是否被调用,但参数未设置? – 2013-04-21 09:20:39
它没有给出任何错误。只是给我看一个空白页面。 – 2013-04-21 09:35:06