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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:42:51+00:00 2026-05-23T11:42:51+00:00

ASP.NET MVC seems to be encouraging me to use hard-coded strings to refer to

  • 0

ASP.NET MVC seems to be encouraging me to use hard-coded strings to refer to controllers and actions.

For example, in a controller:

return RedirectToAction("Index", "Home");

or, in a view:

Html.RenderPartial("Index", "Home");

I don’t want hard-coded strings all over my code. What can I do to avoid this?

  • 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-23T11:42:52+00:00Added an answer on May 23, 2026 at 11:42 am

    It sounds to me like you want to use strongly typed redirects. I made a static helper class called RedirectionHelper that has the following method:

    public static string GetUrl<T>(Expression<Action<T>> action, RequestContext requestContext, RouteValueDictionary values = null) where T : Controller
    {
        UrlHelper urlHelper = new UrlHelper(requestContext);
        RouteValueDictionary routeValues = ExpressionHelper.GetRouteValuesFromExpression(action);
    
        if (values != null)
            foreach (var value in values)
                routeValues.Add(value.Key, value.Value);
    
        return urlHelper.RouteUrl(routeValues);
    }
    

    The only caveat is that you will have to use the Microsoft.Web.Mvc futures library available out on Nuget.

    Now, for your controller, create a base controller that all controllers inherit from that has this method:

    protected RedirectResult RedirectToAction<T>(Expression<Action<T>> action, RouteValueDictionary values = null) where T : Controller
    {
        return new RedirectResult(RedirectionHelper.GetUrl(action, Request.RequestContext, values));
    }
    

    Now, in your action, all you have to do is say:

    return RedirectToAction<Controller>(x => x.Index());
    

    Likewise, you can write a html extension method that takes in the same parameters and builds your anchor tag.

    Like you said above that you wanted, when you change Controller or Action names, your project will break at compile time and show you where the breaks occur. However, this will only occur in the controllers, seeing as how the views don’t compile.

    Hope this helps!

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

Sidebar

Related Questions

In ASP.NET MVC, we're required to use the suffix Controller for all controllers. This
Why do ASP.NET MVC HTML Helpers return MvcHtmlString instances? It seems rather inefficient to
in asp.net mvc, it seems like checkboxes bind to array of strings (if they
In ASP.NET MVC it seems to be common practice not to use GET requests
The standard template for ASP.NET MVC (and MVC in general) seems to be {controller}/{action}/{id}
I am trying to figure out how to use ASP.NET MVC and it seems
ASP.NET MVC seems to correctly automatically bind between HTML form's file input field and
I'm learning asp.net mvc and found something interesting: It seems that I can't explicitly
I noticed what seems to me a bug in asp.net MVC or simply I
As far as theory goes it seems that Asp.net MVC framework could better be

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.