Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 551261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:22:29+00:00 2026-05-13T11:22:29+00:00

My skills are failing me, and I know I’ve seen the code around for

  • 0

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 😉

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:22:30+00:00Added an answer on May 13, 2026 at 11:22 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My google skills are failing me on this. I'm looking for the right way
my jQuery skills are pretty good normally but this is driving me mad! It's
My C# skills are low, but I can't understand why the following fails: public
My googling skills are failing me. My understanding is that there is a way
I've reached my skills limit here. I don't even know if this is possible
I think my searching skills are terrible today, but I am trying to find
I have working VBA code, but I can't convert it to C#. I have
I know that my Googling skills are failing me because there must be something
My google-fu skills doesn't seem to fetch any absolute result. I have developed this
my SQL skills aren't strong enough to figure this out, hope someone can help.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.