我也希望能夠生成一個控制檯應用程序(站點地圖生成器)從路由集合的網址,並有太多的路線,做簡單的換人。我最後寫一個包裝圍繞UrlHelper.GenerateUrl ...
public class UrlGenerator
{
protected string Protocol;
protected string HostName;
protected RequestContext RequestContext;
public RouteCollection RouteCollection;
public UrlGenerator(string protocol, string hostName, Action<RouteCollection> registerRoutes)
{
Protocol = protocol;
HostName = hostName;
RouteCollection = new RouteCollection();
registerRoutes(RouteCollection);
// Construct a request context with as little as possible
RequestContext = new RequestContext(new HttpContextWrapper(new HttpContext(new HttpRequest(null, "http://x.com", null), new HttpResponse(null))), new RouteData());
}
public string GetUrl(string action, string controller, object routeData)
{
return GetUrl(action, controller, new RouteValueDictionary(routeData));
}
public string GetUrl(string action, string controller, RouteValueDictionary routeData)
{
return UrlHelper.GenerateUrl(null, action, controller, Protocol, HostName, null, routeData, RouteCollection, RequestContext, false);
}
}
現在你可以做...
UrlGenerator g = new UrlGenerator("https", "myurl.com", RouteConfig.RegisterRoutes);
Console.WriteLine(g.GetUrl("Index", "Home", new { product = "toad-for-oracle" }));
不是你問這裏完全相信。您可以訪問應用程序之外的任何路線。 –
你是說你想把它添加到MVC應用程序的路由表嗎? – Dhawalk