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

1
  • 1
    See ASP.NET MVC 5 culture in route and url. 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). Commented Oct 11, 2017 at 15:59

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

No problem, glad I could help you
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.