Sac*_*nth 8 asp.net-mvc routes c#-4.0 asp.net-mvc-3
我有一个名为Account的控制器,其操作具有以下签名:
public ActionResult Verify(string userName, Guid authorisationToken);
Run Code Online (Sandbox Code Playgroud)
我创建了一个链接来调用此操作:
/Account/Verify/sachin13/409bdaaa-0b65-4bb8-8695-6e430323d8f8
Run Code Online (Sandbox Code Playgroud)
当我转到此链接时,我收到以下错误:
The constraint entry 'Length' on the route with URL 'Account/{Verify}/{userName}/{authorisationToken}' must have a string value or be of a type which implements IRouteConstraint.
Run Code Online (Sandbox Code Playgroud)
这是我的RegisterRoutes方法在Global.asax中的样子:
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
new[] { "UI.Controllers" }
);
routes.MapRoute(
"AccountVerify",
"Account/{Verify}/{userName}/{authorisationToken}",
new { controller = "Account", action = "Verify", userName = "", authorisationToken = "" },
"UI.Controllers"
);
}
Run Code Online (Sandbox Code Playgroud)
两个问题:
我做了什么与众不同的事情,还是我的方法符合标准做法?
这里有什么问题?
谢谢,
萨钦
Ser*_*sev 13
你应该改变
"UI.Controllers"
至
new[] { "UI.Controllers" }
在你的第二条路线.
如果你只指定一个字符串(不是一个数组),那么你会得到错误的MapRoute函数重载- 而不是MapRoute(RouteCollection, String, String, Object, String[])接受一个名称空间列表作为最后一个参数,MapRoute(RouteCollection, String, String, Object, Object)它将约束作为最后一个参数.字符串"UI.Controllers"不是正确的约束规范=>您得到错误.
此外,@ Pankaj建议您的自定义路由应在默认值之前运行,并且验证应该没有"{}".
完整代码:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"AccountVerify",
"Account/Verify/{userName}/{authorisationToken}",
new { controller = "Account", action = "Verify", userName = "", authorisationToken = "" },
new [] { "UI.Controllers" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } ,// Parameter defaults
new[] { "UI.Controllers" }
);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7954 次 |
| 最近记录: |