ASP.NET MVC: Application_Start and Url.Action

I struggle with MVC - what I like - and these are the traits. I am trying to load a menu into the Application_Start event. I want to download some links with the correct URL (controller name / actionName), but I cannot use Url.Action or other methods to create the path.

Can someone help me?

+4
source share
2 answers

Why do you want to create your own menu in application_start? Is this for some kind of caching? Anyway, here it goes.

RegisterRoutes(RouteTable.Routes); var httpContext = new HttpContextWrapper(HttpContext.Current); UrlHelper urlHelper = new UrlHelper( new RequestContext(httpContext, new RouteData())); var urlToHome = urlHelper.RouteUrl("Home"); 

I would rather do a RenderAction on your main page, which indicates an action that is being cached, or something like that.

+5
source
 protected void Application_Start() { RegisterRoutes(RouteTable.Routes); var context = new HttpContextWrapper(HttpContext.Current); var routeData = RouteTable.Routes.GetRouteData(context) ?? new RouteData(); var requestContext = new RequestContext(context, routeData); var urlHelper = new UrlHelper(requestContext); var url = urlHelper.Action("Home", "Index"); // TODO: do something with the url } 
+5
source

All Articles