4

I am struggling with MVC - which I love - and it's features. I am trying to load a menu in the Application_Start event. I want to load some links with the correct url (controllerName/actionName) but I can't use the Url.Action or other methods to build the path.

Can anybody help me?

0

2 Answers 2

5

Why would you want to build your menu in the application_start? Is it for some kind of caching? Anyway here 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 recommend doing a RenderAction on your masterpage what points to a action that is cached, or something like that.

Sign up to request clarification or add additional context in comments.

1 Comment

note: fails in integrated mode : blogs.iis.net/mvolo/archive/2007/11/10/…
5
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
}

1 Comment

note: fails in integrated mode : blogs.iis.net/mvolo/archive/2007/11/10/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.