在 MVC 中创建多语言未找到页面

2023-11-22

我们有一个多语言网站,包含四种语言的内容。每种语言都可以通过我们在网址第一个添加的语言名称来理解。 这是我们的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

另外,在我们的控制器中,我们从 url 获取语言名称,并根据它更改 currentCulture 和 currentUiCulture。 现在,我们希望有一个所有语言的未找到页面。通常,为了实现这一点,我们添加一个错误控制器和一个 NotFound 操作和视图,然后在 web.config 中添加此部分:

  <customErrors mode="On" defaultRedirect="error">
  <error statusCode="404" redirect="error/notfound" />
  <error statusCode="403" redirect="error/forbidden" />
</customErrors>

我们添加了一个 NotFound 页面,我们在其中使用 .resx 文件来制作 rtl/ltr 并以四种语言显示消息。 但这里的问题是,在多语言网站中,我们不允许使用“error/notfound”这个地址,因为其中没有语言名称,并且我们不知道如何在redirect =“error/notfound”中添加语言名称在 web.config 文件中创建类似“en/error/notfound”或“fa/error/notfound”的内容。 每一个帮助将不胜感激


首先,看一下这个答案有关通过 URL 本地化您的网站的信息。

Next, <customErrors>是 ASP.NET 错误消息的总括。但一般来说,您可以通过使用包罗万象的路由来控制 ASP.NET MVC 中的 404(路由未命中)。在这种情况下,您可以简单地本地化 catch-all 路由并在 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" }
        );
    }
}

错误控制器

public class ErrorController : Controller
{
    public ActionResult PageNotFound()
    {
        Response.CacheControl = "no-cache";
        Response.StatusCode = (int)HttpStatusCode.NotFound;

        return View();
    }
}

这可以解决 ASP.NET 中的路由缺失问题。对于那些不使用 ASP.NET 的用户(假设您使用 IIS 进行托管),您应该使用<httpErrors>web.config 部分而不是<customErrors>. <httpErrors>可以通过本地化prefixLanguageFilePath setting.

可选字符串属性。

指定生成自定义错误的路径时的初始路径段。该段出现在自定义错误路径的特定于语言的部分之前。例如,在路径 C:\Inetpub\Custerr\en-us\404.htm 中,C:\Inetpub\Custerr 是前缀LanguageFilePath。

<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>

这意味着您需要设置带有语言前缀的文件结构,并使用静态文件作为目标。

C:\Contoso\Content\errors\fa\404.htm
C:\Contoso\Content\errors\en\404.htm

AFAIK,不幸的是,这意味着您需要在这些位置拥有物理文件。但是,您可以将这些页面的内容设置为执行元刷新重定向andJavaScript 重定向到正确的控制器操作。

<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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 MVC 中创建多语言未找到页面 的相关文章

随机推荐