Youku KMSFan Youku KMSFan - 7 months ago 49
C# Question

How to get full default route url?

How to avoid get "/"(root path),I want to get the full path like home/Index(as my default route)?

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);

but all of above is not working. thanks!


This code can be used in order to get the default path in general cases:

var route = ((Route)RouteTable.Routes["Default"]).Defaults;
var url = string.Format("/{0}/{1}", route["controller"], route["action"]);

In case of special RouteMaps the format string ( /{0}/{1} ) should be changed.