有一种方法可以在 OnActionExecuting ASP MVC 4 上重命名控制器吗?
There is a way to rename the controller on OnActionExecuting ASP MVC 4?
我已经尝试根据当前语言创建 url 重写器。例如,如果我有控制器文章,对于每种语言,我都有一条记录:
public class ControllerRewriter
{
public LanguageName { get; set; }
public NiceName { get; set; }
public Controller { get; set; }
}
还有我搜索的 ControllerRewriter 列表。
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;
}
}
在基本控制器中class我重写方法OnActionExecuting以拦截控制器的niceName然后用控制器的真实名称重写它,但我失败了...
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);
}
你有什么idea/sugestion关于我如何重写控制器名称,在 OnActionExecuting 中或可能在其他方法中...?
更新:RedirectToRouteResult 不是一个选项,因为它会改变当前 url。 :-)
谢谢,
奥维迪乌
你可以做类似
filterContext.Result = new RedirectToRouteResult(routeName, routeValues);
我已经尝试根据当前语言创建 url 重写器。例如,如果我有控制器文章,对于每种语言,我都有一条记录:
public class ControllerRewriter
{
public LanguageName { get; set; }
public NiceName { get; set; }
public Controller { get; set; }
}
还有我搜索的 ControllerRewriter 列表。
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;
}
}
在基本控制器中class我重写方法OnActionExecuting以拦截控制器的niceName然后用控制器的真实名称重写它,但我失败了...
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);
}
你有什么idea/sugestion关于我如何重写控制器名称,在 OnActionExecuting 中或可能在其他方法中...?
更新:RedirectToRouteResult 不是一个选项,因为它会改变当前 url。 :-)
谢谢, 奥维迪乌
你可以做类似
filterContext.Result = new RedirectToRouteResult(routeName, routeValues);