Jas*_*ans 4 parameters asp.net-mvc asp.net-mvc-routing
我正在学习如何在ASP.NET MVC中创建自定义路由并且遇到了障碍.在我的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
);
// My Custom Route.
routes.MapRoute(
"User_Filter",
"home/filter/{name}",
new { controller = "Home", action = "Filter", name = String.Empty }
);
}
Run Code Online (Sandbox Code Playgroud)
我的想法是能够导航到http://localhost:123/home/filter/mynameparam.这是我的控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Filter(string name)
{
return this.Content(String.Format("You found me {0}", name));
}
}
Run Code Online (Sandbox Code Playgroud)
当我导航到http://localhost:123/home/filter/mynameparam控制器方法Filter被调用时,但参数name始终是null.
有人可以给出一个指针,指出我构建自定义路由的正确方法,以便将url中的名称部分传递给name参数for Filter().
该Default路线应该是最后一个.试试这种方式:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// My Custom Route.
routes.MapRoute(
"User_Filter",
"home/filter/{name}",
new { controller = "Home", action = "Filter", name = String.Empty }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2037 次 |
| 最近记录: |