2017-02-16 1 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 페이지로 이동합니다. 그것은 독일어로 머 무르지 않습니다. 차이점은 주소 표시 줄에 ".....? aspxerrorpath = ....."대신 "URL을 잘못 입력하면 아무 것도 바뀌지 않습니다"입니다. Btw이 코드는 라우팅의 culture 부분 (localhost : xxxx/en/및 localhost : xxxx/de /)을 어떻게 사용 하는지를 볼 수 없습니다. – Pero93