我是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