2015-01-30 2 views
0

나는 현재 언어에 따라 URL rewriter를 만들려고한다.OnActionExecuting ASP MVC 4에서 컨트롤러의 이름을 바꾸는 방법이 있습니까?

public class ControllerRewriter 
{ 
    public LanguageName { get; set; } 
    public NiceName { get; set; } 
    public Controller { get; set; } 
} 

그리고 또한 내가 검색을 만든 ControllerRewriter의 목록 : 내가 컨트롤러 제이있는 경우 예를 들어, 각 언어에 대해 나는 같은 기록이있다. 내가 컨트롤러의 niceName을 차단하기 위해 메서드 OnActionExecuting을 무시하고 다음 컨트롤러의 실제 이름으로 다시,하지만 난 실패 기본 컨트롤러 클래스에서

public class ControllerRewriterList: List<ControllerRewriter> 
{ 
    public string GetController(string niceName) 
    { 
     var cr = this.FirstOrDefault(c => c.NiceName == niceName); 
     if(cr == null) 
      return niceName; 
     else 
      return cr.Controller; 
    } 
} 

...

protected override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     _actionName = filterContext.ActionDescriptor.ActionName.ToLower(); 
     //here I get a nice name of controller according with the current language 
     _controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName.ToLower(); 

     //rewrite the controller with the real name of controller 
     //this line doesn't work AND I NEED A SOLUTION (thanks!) 
     filterContext.ActionDescriptor.ControllerDescriptor.ControllerName = crList.GetController(_controllerName); 

     base.OnActionExecuting(filterContext); 
    } 

OnActionExecuting 또는 다른 방법으로 컨트롤러 이름을 다시 쓸 수있는 방법에 대한 아이디어/sugestion이 있습니까?

업데이트 : RedirectToRouteResult는 현재 URL을 변경하므로 옵션이 아닙니다. :-)

감사합니다, 인 Ovidiu

+0

처럼 뭔가를 할 수 난 당신이 OnActionExecuting와 바른 길에 생각합니다. 저는 옛날에 그 방법으로 행동과 조절기를 전환 한 것을 기억합니다 (제가 remembmber 한 것). 코드를 찾을 수 있는지 보겠습니다. – Sandman

답변

0

당신은

filterContext.Result = new RedirectToRouteResult(routeName, routeValues); 
+0

네, 그 해결책을 알고 있지만, 그것은 나를 리디렉션 할 것입니다 ... 그리고 그것은 "추한 사람"과 현재 (좋은) URL을 바꿀 것이고 나는 이것을 원하지 않습니다. –

관련 문제