【问题标题】:Bilingual 404 page in MVC4MVC4 中的双语 404 页面
【发布时间】:2017-02-16 22:43:22
【问题描述】:

我关注了this answer 以获得一个正常运行的双语 MVC4 网站。我制作了也是双语的 404 错误页面,但是当我输入错误的链接时,它要么转到英文版,要么显示 MVC 库存 404 页面。我尝试了很多解决方案,包括两种解决方案found here,但似乎没有任何帮助。

【问题讨论】:

    标签: asp.net-mvc-4 http-status-code-404 multilingual


    【解决方案1】:

    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 视图,您在上面输入的任何内容作为您创建的视图

    它对我有用。

    【讨论】:

    • 试过了,然后它再次进入默认的英文 404 页面。它不会留在德语中。唯一的区别是:在地址栏中输入错误的 URL 后没有任何变化,而不是显示“.....?aspxerrorpath=.....”。顺便说一句,我看不出这段代码如何利用路由的文化部分(localhost:xxxx/en/ 和 localhost:xxxx/de/)
    【解决方案2】:

    成功地让它工作。感谢@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。

    【讨论】:

      猜你喜欢
      • 2012-10-26
      • 2013-05-03
      • 1970-01-01
      • 1970-01-01
      • 2014-02-19
      • 2011-04-06
      • 2018-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多