1

I need to build a c# MVC portal that is localized.

I have the following route defined:

routes.MapRoute(
name: "DefaultLocalized",
url: "{lang}/{controller}/{action}/{id}",
constraints: new { lang = @"(\w{2})|(\w{2}-\w{2})" },   // en or en-US
            defaults: new { controller = "Home", action = "Index", id = 
UrlParameter.Optional }
);

Also implemented a LocalizedControllerActivator.

All works great.

Defaults urls:

http://test

http://test/about ...

When language changed to German it routes to

http://test/de

http://test/de/about ...

that is great.

The issue I have is with images.

for the following line

<img src="media/images/backgrounds/bg.jpg" alt="Smiley face" height="42" 
width="42">

it would resolve to http://test/de/media/images/backgrounds/aria_bg3.jpg

I would like to simply use

http://test/media/images/backgrounds/bg3.jpg

and ignore the language string all together

or I would like to have a different structure like

http://test/media/de/images/backgrounds/bg3.jpg

Any help would be appreciated.

Thanks, Moz

Mozzak
  • 201
  • 3
  • 16
  • 1
    See [ASP.NET MVC 5 culture in route and url](https://stackoverflow.com/a/32839796/). The reason why the URL is being generated this way is because your constraint always matches this route when generating the URL (same as the other answer). – NightOwl888 Oct 11 '17 at 15:59

2 Answers2

1

Try to prepend the src with a /. It will give you the base path.

So for example if thats/a/test is resolved under url https://www.google.com/images, the resulting URL will be https://www.google.com/images/thats/a/test, but when you use /thats/a/test, the resulting URL will be https://www.google.com/thats/a/test.

I think this would fix your problem.

Twometer
  • 1,561
  • 2
  • 16
  • 24
1

Use relative path to the root of your website. Try to start your attribut "src" by "/", like this :

<img src="/media/images/backgrounds/bg.jpg" alt="Smiley face" height="42" width="42">

An other solution is to use html base tag in the head of your webpage, like this :

<head>
    <base href="http://test/" target="_blank">
</head>
Beedjees
  • 584
  • 4
  • 9