Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually instantiate a Controller instance from an arbitrary URL?

My skills are failing me, and I know I've seen the code around for this but I can't find it.

What's the quickest way to take any arbitrary URL, run it through your asp.net mvc routing system, and come out with a reference to a controller instance on the other end?

For example, code execution is inside some arbitrary controller method. I want to do something like this:

...
string myURL = "http://mysite/mycontroller/myaction/myparameters";

RouteData fakeRouteData = new RouteData(Route???, IRouteHandler???)
RequestContext ctxt = new RequestContext(this.ControllerContext.HttpContext,
                                         fakeRouteData);

ControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
Controller result = factory.CreateController(ctxt, controllername???)

I'm trying to get an instance of a controller just like the routing system does, regardless of where the code is executing. I'm unclear as to how to fit the pieces together at this point. While I will eventually discover it, I thought I could save time by asking here ;)

like image 537
womp Avatar asked Jan 14 '10 18:01

womp


People also ask

How are controllers instantiated?

New controller is instantiated on each HTTP request. The instantiation is in-directed, means first we retrieve Type, after creating Instance. The type is being searched dynamically by getting out reflection information from assemblies.

What is controller in URL?

The Controller in MVC architecture handles any incoming URL request. The Controller is a class, derived from the base class System. Web. Mvc.

How do I add a controller in Visual Studio?

The easiest way to create a new controller is to right-click the Controllers folder in the Visual Studio Solution Explorer window and select the Add, Controller menu option (see Figure 1). Selecting this menu option opens the Add Controller dialog (see Figure 2).

How would you setup the default route using endpoints?

UseEndpoints(endpoints => { endpoints. MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); Inside the call to UseEndpoints() , we use the MapControllerRoute() method to create a route by giving the name default .


1 Answers

Hmm... I don't know if this is the best solution because it requires mocking, but maybe this will help. You're on the right track and the controller factory part is simple once you know what controller to instantiate, so the question is what's the fastest way to get a RouteData object from an arbitrary url.

And the only way I know how would be like so, with Moq:

string url = "~/Account/LogOn";  //trying to create Account controller in default MVC app

RouteCollection rc = new RouteCollection();
MvcApplication.RegisterRoutes(rc);
System.Web.Routing.RouteData rd = new RouteData();
var mockHttpContext = new Moq.Mock<HttpContextBase>();
var mockRequest = new Moq.Mock<HttpRequestBase>();
mockHttpContext.Setup(x => x.Request).Returns(mockRequest.Object);
mockRequest.Setup(x => x.AppRelativeCurrentExecutionFilePath).Returns(url);

RouteData routeData = rc.GetRouteData(mockHttpContext.Object);

string controllerName = routeData.Values["controller"].ToString();

IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
IController controller = factory.CreateController(this.ControllerContext.RequestContext, controllerName);

I did quite a bit of googling and couldn't find much that didn't pertain to unit testing/mocking. I don't know if there is a quick and easy to do this, but would certainly like to know if someone has a better solution!

like image 68
Kurt Schindler Avatar answered Oct 05 '22 05:10

Kurt Schindler