2011-03-17 4 views
1

URL 내에 언어를 사용하려면 정확한 경로 설정 방법을 배웠습니다. .../en/MyController/MyMethod. URL, 라우팅 및 지역의 언어

 routes.MapRoute("Default with language", "{lang}/{controller}/{action}/{id}",  
     new 
     { 
      controller = "Report", 
      action = "Index", 
      id = UrlParameter.Optional, 
     }, new { lang = "de|en" }); 
     // Standard-Routing 
     routes.MapRoute("Default", "{controller}/{action}/{id}", new 
     { 
      controller = "Report", 
      action = "Index", 
      id = UrlParameter.Optional, 
      lang = "de", 
     }); 

지금 나는 새로운 영역 Cms을 삽입, 나는() 위해 Application_Start에 AreaRegistration.RegisterAllAreas(); 전화 : 다음 라우팅이 큰 지금까지 작동합니다.

 MvcHandler handler = Context.Handler as MvcHandler; 
     if (handler == null) 
      return; 

     string lang = handler.RequestContext.RouteData.Values["lang"] as string; 

이 어떻게 지역과 위의 라우팅 작업을 할 수 있습니다 :

는 최대한 빨리이 지역 내에서 컨트롤러를 부르는, 나는 언어 키를 그리워? 어떤 계약 및 프로모션 추천 자료 비즈니스 디렉토리 리뷰에 대한

들으, [AreaName]AreaRegistration 이름 AreaRegistration에서 파생 생성 된 클래스를

답변

0

다음 라우팅 (지역이 Cms라고합니다) 내 경우에는 지금 작동 :

using System.Web.Mvc; 

namespace MyProject.Areas.Cms 
{ 
    public class CmsAreaRegistration : AreaRegistration 
    { 
     public override string AreaName 
     { 
      get 
      { 
       return "Cms"; 
      } 
     } 

     public override void RegisterArea(AreaRegistrationContext context) 
     { 

     context.MapRoute("Cms_default_with_language", "Cms/{lang}/{controller}/{action}/{id}", new 
     { 
      controller = "Home", 
      action = "Index", 
      id = UrlParameter.Optional, 
      lang = "de", 
     }, new { lang = "de|en" }); 
     context.MapRoute(
      "Cms_default", 
      "Cms/{controller}/{action}/{id}", 
      new { action = "Index", id = UrlParameter.Optional, lang = "de" } 
     ); 

     } 
    } 
} 

유일한

,이 기본 그것은뿐만 아니라 경로 등록을 포함

입니다 나는별로 행복하지 않습니다. 이제 Global.asax와이 클래스에서 중복 코드가 생깁니다. 이러한 중복 매핑을 피할 수있는 방법이 있습니까?

+0

나는 이와 같은 업데이트로 자신의 질문에 대답 할 생각이 없다고 생각합니다. 어쨌든 . 귀하의 Area Route 등록은 AreaRegistration에서 'AreaName'이 필요하다고 생각합니다. 그래서 이것이 거기에서 재정의되어야합니다. –

+1

@ 마이크, 왜 내 질문에 대답하지 않습니까? 기본적으로 그것은 내 대답에 대한 완벽한 솔루션을 형성합니다. – sl3dg3

1

체크 아웃 sl3dg3.

context.MapRoute(
    "AreaName_default", 
    "AreaName/{controller}/{action}/{id}", 
    new { action = "Index", id = UrlParameter.Optional } 
);