tags:

views:

66

answers:

1

I'm trying to URLs from the LinkBuilder in Microsoft.Web.Mvc. AT the moment, I have:

LinkBuilder.BuildUrlFromExpression(???, RouteTable.Routes, x => x.Index())

But I'm not sure how to get the request context in all cases. E.g. If I'm in a IHttpModule, is it possible for me to somehow get the request context so I can create a URL like this?

A: 

If you are using System.Web.Routing for your MVC setup, you can implement an IRouteHandler (example) which will return a new instance of your custom IHttpHandler class. Since the logic for instantiating the HttpHandler goes inside the GetHttpHandler method of the IRouteHandler, and the IRouteHandler has access to the route request context data, you can pass that in to your HttpHandler's constructor and use it appropriately.

Rex M
Perhaps I'm missing something, but I don't see how this helps when trying to create a URL with LinkBuilder in say an HttpModule or even a traditional code behind.
bingle
@Nick the LinkBuilder uses the RequestContext from System.Web.Routing. You must be using routing to get that RequestContext. I explained how to create an HttpHandler that can be used in a routing context.
Rex M