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 6846923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:40:56+00:00 2026-05-27T00:40:56+00:00

With the UrlHelper in MVC3, you can build a URL for a controller and

  • 0

With the UrlHelper in MVC3, you can build a URL for a controller and an action using strings:

@Url.Action("Index", "Somewhere")

Which will route a request to SomewhereController.Index().

What I would like to do, is get a URL to a controller and action, but passing the Type for the controller:

@Url.Action("Index", typeof(SomewhereController))

Is there a way to do this?


Edit / Clarification:

I realize the convention for the Controllers is that the controller name routes to a class named {Name}Controller so I could just remove ‘Controller’ from the end of my Type.Name. I guess I was assuming that there was a way to override this convention with some custom routing. Though the more I look at it, I’m not sure that is possible…

Maybe MVC3 can only ever route to classes named “*Controller”? I’m combing through the MVC3 source looking for “Controller” hard coded somewhere, but haven’t found the answer yet… But if it is possible to route “Somewhere” to the class SomewhereFoo instead of SomewhereController, then just removing “Controller” from the class name would be incorrect.

If someone can give me evidence for or against “Controller” being hard-coded into MVC3 somewhere, then I would feel more comfortable with the “Just remove Controller from the name” approach.

  • 1 1 Answer
  • 0 Views
  • 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-27T00:40:57+00:00Added an answer on May 27, 2026 at 12:40 am

    There is no existing extension for this but you could write your own, modeled on the ActionLink from MvcFutures. I suggest a generic method used like @Url.Action<SomewhereController>( c => c.Index )

    public static UrlHelperExtensions
    {
        public static string Action<TController>( this UrlHelper helper,  Expression<Action<T>> action ) where TController : Controller
        {
            var routeValues = GetRouteValuesFromExpression( action ); 
            return helper.Action( routeValues["action"], routeValues );
        }
    
        // copied from MvcFutures
        // http://aspnet.codeplex.com/SourceControl/changeset/view/72551#266392
        private static RouteValueDictionary GetRouteValuesFromExpression<TController>(Expression<Action<TController>> action) where TController : Controller
        {
            if (action == null) {
                throw new ArgumentNullException("action");
            }
    
            MethodCallExpression call = action.Body as MethodCallExpression;
            if (call == null) {
                throw new ArgumentException(MvcResources.ExpressionHelper_MustBeMethodCall, "action");
            }
    
            string controllerName = typeof(TController).Name;
            if (!controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)) {
                throw new ArgumentException(MvcResources.ExpressionHelper_TargetMustEndInController, "action");
            }
            controllerName = controllerName.Substring(0, controllerName.Length - "Controller".Length);
            if (controllerName.Length == 0) {
                throw new ArgumentException(MvcResources.ExpressionHelper_CannotRouteToController, "action");
            }
    
            // TODO: How do we know that this method is even web callable?
            //      For now, we just let the call itself throw an exception.
    
            var rvd = new RouteValueDictionary();
            rvd.Add("Controller", controllerName);
            rvd.Add("Action", call.Method.Name);
            AddParameterValuesFromExpressionToDictionary(rvd, call);
            return rvd;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got public MvcHtmlString(this UrlHelper url) { string controller = //??? string action =
Hi, Im using the following to generate full URL : urlHelper.Action(Detail, Ad, null, http)
I am using MenuBuilder with code like this: public static MenuItem MainMenu(UrlHelper url) {
So I have a route in routes.rb like this: get customers/:id/payments, :controller=>customers, :action=>payments What
Hi, I am using the following code to generate a URL : UrlHelper urlHelper
How can I generate a URL by RouteName outside of a view or controller?
I'm having problems retrieving the route url from controller.RouteUrl(routeName) method. Here is my code
why defined extension method with UrlHelper don't added in Url. EXTENSIONMETHOD when i want
You can use the URL helper in Code Igniter to load CSS and Javascript
I'm having difficultly adding querystring parameters to link_to UrlHelper. I have an Index view,

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.