2017-02-16 8 views
0

機能しているバイリンガルMVC4サイトでは、this answerが続きました。私は404のエラーページを作りました。これもバイリンガルですが、間違ったリンクを入力すると、英語版に行くか、MVC在庫404ページが表示されます。私は両方のソリューションを含む多くのソリューションを試したfound here、しかし何も助けているようだ。MVC4でのバイリンガル404ページ

答えて

0

正常に動作するようにしました。 @ r.vengadeshと私が質問で提供したリンクに感謝します。

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" } 
     ); 
    } 
} 

とにかく、ここで...

のGlobal.asax内のファイルや修正がありますErrorControllerの中にページ404があります。

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); 
     } 

そして、ちょうどエラーコントローラを作成し、ビューとして上記のように入力したものは何でもNotFound404ビューを作成するには、作成し、それが私のために働い

+0

これを試してみて、デフォルトの英語404ページに移動します。それはドイツ語ではありません。差異だけです:アドレスバーに、間違ったURLを置いた後に何も変わらず、 ".....?aspxerrorpath = ....."と表示されます。 Btw私はどのようにこのコードは、ルーティング(localhost:xxxx/en /とlocalhost:xxxx/de /)の文化の部分を使用しているか分かりません。 – Pero93