2

Does any body knows how to hide some parameters in url?

For example you have a url parameter "culture". It can be "en, fr, it". By default your site renders in "en" culture and we don't wont to show default culture in the URL but in cases of other cultures parameter "culture" must appear in URL.

http://myrendersite.net/barbikueue
http://myrendersite.net/fr/barbikueue

This is same pages in different cultures. How to do what basing on default asp.net mvc routing system?

tereško
  • 58,060
  • 25
  • 98
  • 150
Ilya
  • 51
  • 3

2 Answers2

2

This will help:

ASP.NET mvc, localized routes and the default language for the user

asp.net mvc localization

Set Culture in an ASP.Net MVC app

You have to register two routes:

routes.MapRoute(
            name: "DefaultLang",
            url: "{language}/{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            constraints: new { language = "[a-z]{2}"}
        );

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

Create an Attribute that inherits ActionFilterAttribute:

public class LanguageActionFilterAttribute : ActionFilterAttribute
    {

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var routeDataKey = "language";
            var defaultLang = "en";
            var defaultCulture = "EN";

            // if no language route param received
            if (filterContext.RouteData.Values[routeDataKey] == null /* && currentCulture != "en-EN" */)
            {
                // change culture to en-EN
                Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", defaultLang, defaultCulture));
                Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", defaultLang, defaultCulture));
            }
            else
            {
                 /*if (currentCulture != "whatever")
                 { 
                    //set culture to whatever
                 }*/
            }

            base.OnActionExecuting(filterContext);
        }
    }

After that create a BaseController with the previous created attribute:

[LanguageActionFilter]
public abstract class BaseController : Controller
{

}

And all your Controllers will inherit BaseController now, instead of Controller

Community
  • 1
  • 1
Cristi Pufu
  • 9,002
  • 3
  • 37
  • 43
  • Thnx for your answer. How we can generate urls to our pages including current language? If current lang is default then we must use one route and then lang is not default we must use another route. – Ilya Apr 19 '13 at 18:23
  • You give a generic information about routing and no answer about url creation with this routing config. But main question is url creation! – Ilya Apr 20 '13 at 09:14
  • have you tried it? You should create urls like `Html.ActionLink()`. The application will save the language route param – Cristi Pufu Apr 22 '13 at 08:40
0

The following RouteConstraint might help,

public class CultureConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values,
                      RouteDirection routeDirection)
    {
        if (routeDirection == RouteDirection.UrlGeneration)
        {
            return values[parameterName] != null &&
                   (route.Defaults[parameterName] == null ||
                    values[parameterName].ToString().ToLower() != route.Defaults[parameterName].ToString().ToLower());
        }
        return true;
    }
}

Use it as,

routes.MapRoute(
    name: "Culture",
    url: "{culture}/{controller}/{action}/{id}",
    defaults: new {culture = "en", controller = "Home", action = "Index", id = UrlParameter.Optional},
    constraints: new {culture = new CultureConstraint()}
);

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

Here the constraint only works on outbound url and discard the route for candidacy when 'culture' in route information equals to default.

I used the simplest implementation because you have not posted your route code, but the idea should work.

hope this helps.

shakib
  • 5,449
  • 2
  • 30
  • 39