MVC4中的双语404页面
Bilingual 404 page in MVC4
我关注了 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 ,但似乎没有任何帮助。
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);
}
并且只需创建一个 Error 控制器并创建 NotFound404 查看您在上面输入的任何内容作为您创建的视图
对我有用。
成功使其工作。感谢@r.vengadesh 和我在问题中提供的链接。无论如何,这里是文件和修改......
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);
}
和 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" }
);
}
}
当然你需要 ErrorController 里面有一个 page404。
我关注了
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);
}
并且只需创建一个 Error 控制器并创建 NotFound404 查看您在上面输入的任何内容作为您创建的视图
对我有用。
成功使其工作。感谢@r.vengadesh 和我在问题中提供的链接。无论如何,这里是文件和修改......
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);
}
和 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" }
);
}
}
当然你需要 ErrorController 里面有一个 page404。