我想知道如何定义这样的路由映射:
{TreePath}/{Action}{Id}
TreeMap 是从数据库动态加载的,如下所示:
'Gallery/GalleryA/SubGalleryA/View/3'
您可以创建自定义路由处理程序来执行此操作。实际路线是包罗万象的:
routes.MapRoute(
"Tree",
"Tree/{*path}",
new { controller = "Tree", action = "Index" })
.RouteHandler = new TreeRouteHandler();
树处理程序查看路径,提取最后一部分作为操作,然后重定向到控制器。操作部分也从路径中删除。添加 {id} 部分应该很简单。
public class TreeRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string path = requestContext.RouteData.Values["path"] as string;
if (path != null)
{
int idx = path.LastIndexOf('/');
if (idx >= 0)
{
string actionName = path.Substring(idx+1);
if (actionName.Length > 0)
{
requestContext.RouteData.Values["action"] = actionName;
requestContext.RouteData.Values["path"] =
path.Substring(0, idx);
}
}
}
return new MvcHandler(requestContext);
}
}
然后您的控制器将按照您的预期工作:
public class TreeController : Controller
{
public ActionResult DoStuff(string path)
{
ViewData["path"] = path;
return View("Index");
}
}
现在你可以像这样调用 URL/Tree/Node1/Node2/Node3/DoStuff
。动作得到的路径是Node1/Node2/Node3
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)