在MVC中创建未找到多语言页面 [英] Creating multilingual not found page in MVC
问题描述
我们有一个多语言网站,其中包含四种语言的内容.每种语言都可以通过在网址的第一个位置添加的语言名称来理解. 这是我们的routeConfig.cs:
We have a multilingual website that has content in four languages.Every language is understood by the language name that we add at the first of our url. This is our routeConfig.cs:
routes.MapRoute(
name: "Default",
url: "{lang}/{controller}/{action}/{id}/{title}",
defaults: new { lang = "fa", controller = "Home", action = "Index", id = UrlParameter.Optional,title = UrlParameter.Optional }
这是生成的网址:/en/ContactUs/Index
and this is generated the url: /en/ContactUs/Index
此外,在我们的控制器中,我们从url中获取语言名称,并基于它更改currentCulture和currentUiCulture. 现在,我们希望有一个未找到所有语言的页面.通常,要实现它,我们添加一个错误控制程序和一个NotFound操作并查看,然后在web.config中添加此部分:
Also, in our controllers we get the language name from url and change the currentCulture and currentUiCulture based on it. Now, we want to have a not found page in all of the languages. Normally, to make it happen we add an error contoller and a NotFound action and view, then we add this section in our web.config:
<customErrors mode="On" defaultRedirect="error">
<error statusCode="404" redirect="error/notfound" />
<error statusCode="403" redirect="error/forbidden" />
</customErrors>
我们添加了一个NotFound页面,我们在其中使用.resx文件制作rtl/ltr并以四种语言显示消息. 但是这里的问题是,在多语言网站中,我们不允许使用该地址错误/未找到",因为其中没有语言名称,并且我们不知道如何在redirect ="error/notfound"中添加语言名称.在web.config文件中创建"en/error/notfound"或"fa/error/notfound"之类的内容. 每一个帮助将不胜感激
We have added a NotFound page that we use .resx files in it to make rtl/ltr and to show the messages in four languages. But the problem here is that in a multilingual website we are not allowed to use this address "error/notfound" because there is no languagename in it and we don't know how to add the language name in redirect="error/notfound" in the web.config file to create something like "en/error/notfound" or "fa/error/notfound". every help would be highly appreciated
推荐答案
web.config中的customErrors
部分是有关某些状态码及其处理方式的静态数据.此部分的职责可以通过Global.asax中的Application_EndRequest
方法动态生成.
The customErrors
section in web.config is the static data about some status-code and how they will be handled. The responsibility of this section can be generated dynamically by the Application_EndRequest
method in Global.asax.
protected void Application_EndRequest()
{
if (Context.Response.StatusCode == 404)
{
Response.Clear();
var routeData = new RouteData();
HttpContextBase currentContext = new HttpContextWrapper(HttpContext.Current);
var lang = RouteTable.Routes.GetRouteData(currentContext).Values["lang"];
routeData.Values["lang"] = lang;
routeData.Values["controller"] = "CustomError";
routeData.Values["action"] = "NotFound";
IController customErrorController = new CustomErrorController();
customErrorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
}
这篇关于在MVC中创建未找到多语言页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!