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.
Asked
Active
Viewed 126 times
2 Answers
0
Global.asax
protected void Application_Error(object sender, EventArgs e)
{
// Do whatever you want to do with the error
//Show the custom error page...
Server.ClearError();
var routeData = new RouteData();
routeData.Values["controller"] = "Error";
if ((Context.Server.GetLastError() is HttpException) && ((Context.Server.GetLastError() as HttpException).GetHttpCode() != 404))
{
routeData.Values["action"] = "Index";
}
else
{
// Handle 404 error and response code
Response.StatusCode = 404;
routeData.Values["action"] = "NotFound404";
}
Response.TrySkipIisCustomErrors = true; // If you are using IIS7, have this line
IController errorsController = new ErrorController();
HttpContextWrapper wrapper = new HttpContextWrapper(Context);
var rc = new System.Web.Routing.RequestContext(wrapper, routeData);
errorsController.Execute(rc);
}
And just create a Error controller and create NotFound404 view whatever you entered in that above as a view you create that
It worked for me.

r.vengadesh
- 1,721
- 3
- 20
- 36
-
Tried this and again it goes to the default English 404 page. It doesn't stay in German. Only difference is: in the address bar nothing changes after the wrong URL is put, instead of showing ".....?aspxerrorpath=.....". Btw I can't see how does this code makes use of the culture part of the routing (localhost:xxxx/en/ and localhost:xxxx/de/) – Pero93 Feb 16 '17 at 12:44
0
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.

Pero93
- 150
- 2
- 11