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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:00:05+00:00 2026-06-18T05:00:05+00:00

I have an MVC4 application that has an extra element in the route/url, it’s

  • 0

I have an MVC4 application that has an extra element in the route/url, it’s named language:

routes.MapRoute(
    name: "languageDefault",
    url: "{language}/{controller}/{action}/{id}",
    defaults: new {
        language = "en-US",
        controller = "Home",
        action = "Index",
        id = UrlParameter.Optional
    }
);

Now, every time an action is done I check if the language is set correctly using a custom filterAttribute:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    string cultureName = null;
    var request = filterContext.HttpContext.Request;
    string currentUrl = request.RawUrl;
    string[] splittedStrings = currentUrl.Split('/');

    string currentLanguage  = CultureHelper.CheckCulture();
    var cultureCookie       = request.Cookies["_culture"];
    var possibleCultures    = UnitOfWork.CulturesRepository.GetListOfCultureNames();

    foreach (string culture in possibleCultures)
    {
        if (splittedStrings[1] == culture) cultureName = splittedStrings[1];
    }
    if (cultureCookie != null) cultureName = cultureCookie.Value;
    if (currentLanguage != null) cultureName = currentLanguage;

    if (cultureName != null)
    {
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
        Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
    }
    else
    {
        cultureName = possibleCultures[0];
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
        Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
    }

    base.OnActionExecuting(filterContext);
}

My problem now is that while I check if there is a language present and if there is not, set one, I don’t really do anything if the language provided isn’t correct. For example
When a user types:

http://www.example.com/en-US/shop

He has provided a correct url and thus will see the shop page correctly. However, if he does not provide a language like so:

http://www.example.com/shop

The check for the language is done, and depending on if this is his first visit or not, his language is set. The problem with this is that the url doesn’t change.

I’d like the url to be changed so that it shows the language in which the user views the page. In other words when trying to visit

http://www.example.com/shop

He should be redirected to:

http://www.example.com/en-US/shop

And this needs to happen regardless of which page the visitor visits. In other words I probably need to add something in either the global.asax or in the overrided function I provided.
But I don’t know what and how.

  • 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-06-18T05:00:06+00:00Added an answer on June 18, 2026 at 5:00 am

    I’ve solved it by changing my OnActionExecuting method:

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var request         = filterContext.HttpContext.Request;
            string currentUrl   = request.RawUrl;
            var urlHelper       = new UrlHelper(request.RequestContext);
    
            string baseurl                      = urlHelper.Content("~");
            string currentLanguageFromUrl       = currentUrl.Split('/')[1];
            string currentLanguageFromCulture   = CultureHelper.CheckCulture();
            var currentLanguageFromCookie       = request.Cookies["_culture"];
    
            var possibleCultures                = UnitOfWork.CulturesRepository.GetListOfCultureNames();
    
            if (possibleCultures.All(culture => currentLanguageFromUrl != culture))
            {
                string cultureName;
                string newUrl;
    
                if (currentLanguageFromCookie != null)
                {
                    cultureName = currentLanguageFromCookie.Value;
                    CultureHelper.SetCulture(cultureName);
                    newUrl = baseurl + cultureName;
                    filterContext.Result = new RedirectResult(newUrl);
                    return;
                }
    
                if (currentLanguageFromCulture != null)
                {
                    cultureName = currentLanguageFromCulture;
                    CultureHelper.SetCulture(cultureName);
                    newUrl = baseurl + cultureName;
                    filterContext.Result = new RedirectResult(newUrl);
                    return;
                }
    
                cultureName = possibleCultures[0];
                CultureHelper.SetCulture(cultureName);
                newUrl = baseurl + cultureName;
                filterContext.Result = new RedirectResult(newUrl);
                return;
            }
    
            base.OnActionExecuting(filterContext);
        }
    

    Now, when you fill in an incorrect language you are redirected to a page with a correct language, if you provide two incorrect parameters you are redirected to a 404 (which is logical).

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

Sidebar

Related Questions

Description: I have a simple form in an MVC4 application that has 5 textboxes
Currently, I have a Desktop application that has some of its views available to
I have a dynamic MVC4, jQuery Mobile application that works for the most part
I have a layered web application that I built with ASP.NET MVC 4, WebAPI
I have a simple MVC 4 (Beta) Application and just observed something that I
I have an MVC4 web application In setting it up on IIS7.5 I added
I am creating a MVC4 application and I have got some troubles when I
I am currently working on MVC4 SinglePage Application. I have a Web Api method
I have been writing an application using ASP.Net MVC4, where the majority of the
I have an MVC4 site with a nice customError page that works great if

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.