1

if i have an route like /foo/bar/pewpew .. is it possible to get an instance of the controller which that route maps too?

H H
  • 263,252
  • 30
  • 330
  • 514
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
  • should be... is `foo` your controller? could you define what each part of your url represents? – Luke Duddridge Aug 08 '11 at 12:46
  • What do you mean with "return a controller" and what _is_ the controller in your example? – H H Aug 08 '11 at 12:46
  • 1
    @Henk: Whatever ASP.Net would instantiate if you navigate to that URL. – SLaks Aug 08 '11 at 12:48
  • Where are you planning to use this instance? the `MvcHandler` does the work of mapping the route values to a controller (which it requests via the `ControllerBuilder`/`DependencyResolver`). You user code really starts within a controller already, so what are you trying to do? A use case would be nice point to start. – Matthew Abbott Aug 08 '11 at 12:48
  • @Matthew Abbot : custom error handling. Previous SO question: http://stackoverflow.com/questions/6972521/how-do-i-execute-a-controller-action-from-an-httpmodule-in-asp-net-mvc – Pure.Krome Aug 08 '11 at 12:52

2 Answers2

4

To get the controller name, you can call create a fake HttpContextBase that returns your URL in its Request, then pass it to RouteTable.Routes.GetRouteData and check the area and controller values.

To get the controller instance, pass a RequestContext consisting of that HttpContextBase and RouteData to ControllerBuilder.Current.GetControllerFactory.CreateController.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • creating a fake `HttpContextBase` :: which (bare minimum) properties will I need to override for `GetRouteData` to not throw an exception? (and the rest of the answer makes sense). – Pure.Krome Aug 08 '11 at 13:03
  • Hmmm .. not sure why it was removing the @SLaks word from my previous comment... ?? – Pure.Krome Aug 08 '11 at 13:13
  • @Pure: You don't need to `@`-address the person who wrote the post you're commenting on. – SLaks Aug 08 '11 at 16:17
  • @Pure:I think all you need is `Request.Url`. Try it and see if you get an exception. – SLaks Aug 08 '11 at 16:18
  • Looking at the source, you only need `AppRelativeCurrentExecutionFilePath` and `PathInfo`, as well as any properties used by custom `IRouteConstraint`s. – SLaks Aug 08 '11 at 16:20
1

Try,

        var wrapper=new HttpContextWrapper(System.Web.HttpContext.Current);
        var routeData = RouteTable.Routes.GetRouteData(wrapper);
        var controller = ControllerBuilder.Current.GetControllerFactory().CreateController(new RequestContext(wrapper, routeData), routeData.Values["controller"].ToString());

Update, you can use this instead.

var wrapper = new HttpContextWrapper(new System.Web.HttpContext(new HttpRequest(null, "http://localhost:4836/", null), new HttpResponse(new StringWriter())));
user571646
  • 665
  • 5
  • 10
  • `System.Web.HttpContext.Current` might not be the url i wish to use. In fact, it will never be the correct url in my scenario's. – Pure.Krome Aug 08 '11 at 14:01