Maj*_*ori 6 asp.net-mvc multilingual asp.net-mvc-routing http-status-code-404
我们有一个多语言网站,其中包含四种语言的内容.我们在网址的第一个添加的语言名称可以理解每种语言.这是我们的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 }
Run Code Online (Sandbox Code Playgroud)
这是生成url:/ en/ContactUs/Index
此外,在我们的控制器中,我们从url获取语言名称,并根据它更改currentCulture和currentUiCulture.现在,我们希望在所有语言中都找到一个未找到的页面.通常,为了实现它,我们添加了一个错误控制器和一个NotFound操作和视图,然后我们在web.config中添加了这个部分:
<customErrors mode="On" defaultRedirect="error">
<error statusCode="404" redirect="error/notfound" />
<error statusCode="403" redirect="error/forbidden" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)
我们添加了一个NotFound页面,我们在其中使用.resx文件来制作rtl/ltr并以四种语言显示消息.但问题是,在多语言网站中,我们不允许使用此地址"错误/未发现",因为其中没有语言名称,我们不知道如何在redirect ="error/notfound"中添加语言名称在web.config文件中创建类似"en/error/notfound"或"fa/error/notfound"的内容.每一个帮助都将受到高度赞赏
首先,请查看此答案以获取有关通过URL本地化网站的信息。
接下来,<customErrors>是ASP.NET错误消息的全部内容。但总的来说,您可以使用全部路由来控制ASP.NET MVC中的404(路由丢失)。在这种情况下,您可以简单地定位所有路由,并在web.config中摆脱此配置。
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Localized-Default",
url: "{lang}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { lang = new CultureConstraint(defaultCulture: "fa", pattern: "[a-z]{2}") }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { lang = "fa", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
// Catch-all route (for routing misses)
routes.MapRoute(
name: "Localized-404",
url: "{lang}/{*url}",
defaults: new { controller = "Error", action = "PageNotFound" },
constraints: new { lang = new CultureConstraint(defaultCulture: "fa", pattern: "[a-z]{2}") }
);
routes.MapRoute(
name: "Default-404",
url: "{*url}",
defaults: new { lang = "fa", controller = "Error", action = "PageNotFound" }
);
}
}
Run Code Online (Sandbox Code Playgroud)
public class ErrorController : Controller
{
public ActionResult PageNotFound()
{
Response.CacheControl = "no-cache";
Response.StatusCode = (int)HttpStatusCode.NotFound;
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
这样可以解决ASP.NET中的路由丢失问题。对于那些没有使用ASP.NET的应用程序(假设您使用IIS进行托管),则应使用<httpErrors>web.config部分而不是<customErrors>。<httpErrors>可通过prefixLanguageFilePath设置本地化。
可选的字符串属性。
在生成自定义错误的路径时,指定初始路径段。此段出现在自定义错误路径的特定于语言的部分之前。例如,在路径C:\ Inetpub \ Custerr \ en-us \ 404.htm中,C:\ Inetpub \ Custerr是prefixLanguageFilePath。
<configuration>
<system.webServer>
<httpErrors errorMode="DetailedLocalOnly" defaultResponseMode="File" >
<remove statusCode="404" />
<error statusCode="404"
prefixLanguageFilePath="C:\Contoso\Content\errors"
path="404.htm" />
</httpErrors>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
这意味着您将需要设置带有语言前缀的文件结构,并将静态文件用作目标。
C:\Contoso\Content\errors\fa\404.htm
C:\Contoso\Content\errors\en\404.htm
Run Code Online (Sandbox Code Playgroud)
AFAIK,不幸的是,这意味着您需要在这些位置上有物理文件。但是,您可以设置这些页面的内容,以执行元刷新重定向和 JavaScript重定向到正确的控制器操作。
<html>
<head>
<title>404 Not Found</title>
<meta http-equiv="refresh" content="1;http://www.example.com/fa/Error/PageNotFound" />
</head>
<body>
<!-- Add localized message (for those browsers that don't redirect). -->
<script>
//<!--
setTimeout(function () {
window.location = "http://www.example.com/fa/Error/PageNotFound";
}, 1000);
//-->
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
web.config 中的部分customErrors是有关某些状态代码及其处理方式的静态数据。本节的职责可以通过Application_EndRequestGlobal.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));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1668 次 |
| 最近记录: |