开发者

Should I replace string patterns in asp.net mvc using a custom viewengine?

开发者 https://www.devze.com 2022-12-24 21:03 出处:网络
Have an ASP.NET MVC site that is localized. The localization functionality adds the two digit language ID to the URL, e.g. /es/Page. If no language Id is found in the URL, the site switches to the use

Have an ASP.NET MVC site that is localized. The localization functionality adds the two digit language ID to the URL, e.g. /es/Page. If no language Id is found in the URL, the site switches to the user's browser culture. All's good. However, the site's hyperlinks, a mixture of hard-coded href tags, actionlinks, etc., don't include the base language ID, so when clicking through the site the set culture is lost, and the site reverts to the user's browser culture.

My (lazy) thought is to replace all href values, that don't point to an external site, with the localized URL (e.g. include the /es/)开发者_如何学运维. Otherwise, all site links will need to be updated to include the culture code.

Is this just plain dumb? Or, reasonable, and should be done using a custom view engine, or some other approach?


My answer is (currently):

The app has a base controller, there I've added:

if (PathLanguageCode == "" && requestContext.HttpContext.Session["LanguageCode"] != null && requestContext.HttpContext.Request.RequestType == "GET")
{
    requestContext.HttpContext.Response.Redirect("/" + requestContext.HttpContext.Session["LanguageCode"] + requestContext.HttpContext.Request.RawUrl);
}

This example doesn't show how the PathLanguageCode variable is defined, but it should at least suffice to show how this can be handled centrally, without replacing string values.

The one downside to this approach, that I can see, is that the site really isn't friendly for search engines, etc., since we end up doing a lot of redirects.


If you would like you can look at my approach that we took. It talks about putting the culture in the URL. Hope it helps!

Cultured View Engine for MVC

0

精彩评论

暂无评论...
验证码 换一张
取 消