How to redirect when route is missing "parts" in asp.net MVC?
Let's say I have a url like this:
www.site.com/en/home/list/2010-09-30
The "en" is for English, and I want to make sure there is always a language token set in the url. If a user enters www.site.com/home/list/2010-09-30
I want it to redirect them to www.site.com/en/home/lis开发者_运维百科t/2010-09-30
(where en is the default language).
How is this best accomplished in asp.net MVC (version if that matters)?
This would probably best be handled in a custom HttpModule. I'm sure you already know, but just in case, you can default the route value "en" in your route declarations as well. It won't redirect the page but the culture will always be set in code if it's not present in the URL.
context.MapRoute(
"Default",
"{culture}/{controller}/{action}/{id}",
new { culture = "en", action = "Index" });
A simple solution would be to have a wildcard route (This should be at the bottom of your global.asax file):
routes.MapRoute(
"WildCard",
"{*url}",
new { controller = "Main", action = "EnglishDefault" }
);
Then your action would be:
public ActionResult EnglishDefault (string url)
{
return Redirect("/en/" + url);
}
精彩评论