Ami*_*aqi 5 asp.net-mvc routing asp.net-mvc-routing asp.net-mvc-4
我有一个名为controller的控制器Registration
和一个action方法,如下所示:
public JsonResult GetReqs(GridSettings gridSettings, int rt)
{
...//whatever
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
所以我添加了一条路线,现在我RouteConfig.cs
是这样的:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "RegReq",
url: "{Registration}/{GetReqs}/{rt}",
defaults: new { controller = "Registration", action = "GetReqs", rt = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Run Code Online (Sandbox Code Playgroud)
但是我无法访问rt
参数并且GetReqs
未调用action方法(我在其上设置了一个断点但没有发生任何事情).哪里出错了?
编辑:链接示例我试过:~/Registration/GetReqs/1
我想你需要删除第一条路线中的括号:
routes.MapRoute(
name: "RegReq",
url: "Registration/GetReqs/{rt}",
defaults: new { controller = "Registration", action = "GetReqs",
rt = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
默认路由必须{controller}
告诉MVC使用url的该部分中的字符串作为控制器名称.你知道控制器,所以你只需要匹配特定的字符串.
归档时间: |
|
查看次数: |
3622 次 |
最近记录: |