如何避免获取" /"(根路径),我想获得像home / Index(作为我的默认路由)的完整路径?
var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;
if (redirectTo.Equals("/"))
{
VirtualPathData vp = RouteTable.Routes.GetVirtualPath(htmlHelper.ViewContext.RequestContext, "Default", new RouteValueDictionary(new { controller = "Admin", action = "Index" }));
redirectTo = vp.VirtualPath;
}
或
UrlHelper u = new UrlHelper(htmlHelper.ViewContext.RequestContext);;
redirectTo = u.RouteUrl("Default", ((Route)RouteTable.Routes["Default"]).Defaults);
但上述所有方法都无效。谢谢!
答案 0 :(得分:2)
此代码可用于获取一般情况下的默认路径:
var route = ((Route)RouteTable.Routes["Default"]).Defaults;
var url = string.Format("/{0}/{1}", route["controller"], route["action"]);
如果是特殊的RouteMaps,则应更改格式字符串(/ {0} / {1})。