Bilingual 404 page in MVC4

Pero93

I followed this answer for a functioning bilingual MVC4 site. I made 404 error page which is also bilingual, but when I enter a wrong link, either it goes to the English version or it shows the MVC stock 404 page. I tried many solutions, including both the solutions found here, but nothing seems to help.

Pero93

Successfully made it to work. Thanks to @r.vengadesh and the links I provided in the question. Anyway, here are the files and the modifications within...

Global.asax:

protected void Application_Error(object sender, EventArgs e)
{
    Server.ClearError();
    var routeData = new RouteData();
    HttpContextBase currentContext = new HttpContextWrapper(HttpContext.Current);
    var culture = RouteTable.Routes.GetRouteData(currentContext).Values["culture"];
    routeData.Values["culture"] = culture;
    routeData.Values["controller"] = "Error";

    if ((Context.Server.GetLastError() is HttpException) && ((Context.Server.GetLastError() as HttpException).GetHttpCode() != 404))
    {
        routeData.Values["action"] = "Index";
    }
    else
    {
        Response.StatusCode = 404;
        routeData.Values["action"] = "page404";
    }
    IController errorController = new ErrorController();
    HttpContextWrapper wrapper = new HttpContextWrapper(Context);
    var rc = new System.Web.Routing.RequestContext(wrapper, routeData);
    errorController.Execute(rc);
}

And the RouteConfig:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "DefaultWithCulture",
            url: "{culture}/{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            constraints: new { culture = new CultureConstraint(defaultCulture: "en", pattern: "[a-z]{2}") }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { culture = "en", controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        // Catch-all route (for routing misses)
        routes.MapRoute(
            name: "Localized-404",
            url: "{culture}/{*url}",
            defaults: new { controller = "Error", action = "page404" },
            constraints: new { culture = new CultureConstraint(defaultCulture: "en", pattern: "[a-z]{2}") }
        );

        routes.MapRoute(
            name: "Default-404",
            url: "{*url}",
            defaults: new { culture = "en", controller = "Error", action = "page404" }
        );
    }
}

Of course you need to have ErrorController with a page404 inside.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Spring MVC: how to intercept 404 before returning error page/response?

Redirect to specific page after session expires (MVC4)

404 error page configuration with Spring 4 annotations

How to link a cshtml page using href in mvc4

Custom Selective 404 Page in MVC6

MVC 6 Routing, SPA fallback + 404 Error Page

Swagger Page is 404 Not Found in MVC net-core

ASP.NET MVC 5 - Error Handle for - 404 Page not found

Calling a Javascript in my razor page from a controller in MVC4

Codeigniter 4 - how to show 404 page?

404 page not found in spring mvc example

Disabled DateTime field is marked as required in MVC4 page

How to make in mvc4 custom validation attribute that alllow to view who is allowed to view page only?

How to solve 404 page using Sitecore MVC?

How to load a page in div dynamically in MVC4

Datepicker didn't work in modal page without adding js code (.Net MVC4)

React - MVC4 .Net - Blank page

Unable to Redirect to different page on click ? mvc4

Displaying a 404 Not Found Page for ASP.NET Core MVC

Not redirecting to login page after session expires in mvc4

Custom routes with custom 404 page in MVC

I need a help in custom mvc login page and dashboard display user details and pic MVC4

Refresh part of page after successful user sign in (ASP.NET MVC4)

Unable to return to login page after session expires in mvc4

ASP.NET MVC 4 Custom 404 Page

MVC Redirection to another page result error 404 resource not found

Auto postBack in MVC4 razor page

Reloading view page each 2 seconds without refresh page on MVC4

spring-mvc-404 page not found error in maven project

TOP Ranking

HotTag

Archive