Creating a multilingual not found page in MVC

We have a multilingual website with content in four languages. Each language is understood by the name of the language that we add in the first of our URLs. 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 }

and this is generated by the URL: / en / ContactUs / Index

In addition, in our controllers, we get the language name from url and change currentCulture and currentUiCulture based on this. Now we want the page not to be in all languages. Usually, for this to happen, we add the error controller and the NotFound action and view, then add this section to our web.config:

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

We added a NotFound page in which we use .resx files to make rtl / ltr and display messages in four languages. But the problem here is that on a multilingual website we are not allowed to use this "error / notfound" address because it does not have a language. We do not 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 will be highly appreciated

+4
source share
3 answers

customErrors web.config - , . Application_EndRequest 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));
    }
}
+3

, URL-.

, <customErrors> - ASP.NET. 404 ( ) ASP.NET MVC catch-all. 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" }
        );
    }
}

ErrorController

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.

.

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

, .

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

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

Source: https://habr.com/ru/post/1622608/


All Articles