Search code examples
c#urlglobalization

Ignore first segment of url for globalization


I was required to change the way the globalisation works on my project.

Today I have

  • www.mysite.com.br
  • www.mysite.com.mx
  • www.mysite.com.ar

So I could access my pages like: www.mysite.com.br/default.aspx

and based on this, I make my way changing the language and the content, based on the URL

Country obj = dataContext.Country.FirstOrDefault(c => c.Domain.Equals(HttpContext.Current.Request.URL.Host.ToLower()));

But now, they said that they need it to be like this

  • www.mysite.com/br
  • www.mysite.com/mx
  • www.mysite.com/ar

So I need to access my pages like: www.mysite.com/br/default.aspx

I could get the Country on my function by using this

Country obj = dataContext.Country.FirstOrDefault(c => c.Abbreviation.Equals(HttpContext.Current.Request.Url.Segments.Where(s => s != "/").First()));

But when I access www.mysite.com/br/Default.aspx he gives me the error "Server Error in '/' Application The resource cannot be found". Of course, I don't have a Default.aspx inside a folder named br.

I was reading about routes. But so far I couldn't implement those and I'm not sure if this is the right path for me atm


By routes, I'm trying something like this:

Global.asax ( I had to create one, because my project didn't have one )

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRoute(
        "Default",
        "{lang}/Default.aspx",
        "~/Default.aspx"
    );
}

But the error is the same.


Solution

  • I got it to work.

    As it is a WebSite not a Project, I had to add RegisterRoutes(RouteTable.Routes); inside Application_Start with the help of this answer