我怎样才能更改路由值然后重定向到这条路? [英] How can I change a route value then redirect to that route?
问题描述
我有一个UserAccountController这需要像这样的路线/ {用户名} / {}行动
。
我想创造一些功能,这样我可以将用户带到一个特定的帐户页面不知道他们的用户名锋线。我希望能够使用的URL /你/ {}行动
这会赶上的事实,你是为自己的用户名发,得到它们的真正用户名(因为他们登录),并重定向他们/他们-实际-的用户名/ {}行动。
我可以在每个控制器动作做到这一点,但我宁愿它发生一些较早的地方,将所有的控制器操作的做到这一点。我试图通过改变 RouteData.Values为此在控制器的
来真正的用户名,然后尝试初始化
法[用户名] Response.RedirectToRoute(的RouteData);到Response.End()
但总是带我去错了地方(一些完全错误的路线)。
更新:
感谢BuildStarted领导我这样的回答:
公共类UserAccountController:控制器
{
保护覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext); 如果((串)filterContext.RouteData.Values [用户名]!=你的)
返回; VAR routeValues =新RouteValueDictionary(filterContext.RouteData.Values);
routeValues [用户名] = UserSession.Current.User.Username;
filterContext.Result =新RedirectToRouteResult(routeValues);
}
}
您可以使用带有IActionFilter的FilterAttribute来完成你想要的东西。
公共类UserFilterAttribute:FilterAttribute,IActionFilter {
公共无效OnActionExecuted(ActionExecutedContext filterContext){
} 公共无效OnActionExecuting(ActionExecutingContext filterContext){
VAR用户名= filterContext.RouteData.Values [用户名];
变种realUserName =; //从数据库负载
filterContext.Result =新RedirectToRouteResult(新System.Web.Routing.RouteValueDictionary(新{控制器=用户,行动=指数,用户名= realUserName}));
}
}
然后在你的控制你的ActionResult您可以应用 [UserFilter]
的动作。
[UserFilter]
公众的ActionResult UnknownUserHandler(){
返回查看();
}
这应该给你你正在寻找的结果。如有任何疑问请留言:)
I have a UserAccountController that takes routes like this "/{username}/{action}"
.
I'd like to create some functionality so that I can take a user to an account-specific page without knowing their username up front. I'd like to be able to use the URL "/your/{action}"
which would catch the fact that "your" was sent as their username, get their real username (because they are logged in), and redirect them to "/their-actual-username/{action}".
I could do this in each of the controller actions, but I'd rather have it happen some place earlier that would do this for all of the controller actions. I attempted to do this in the Controller's Initialize
method by changing the RouteData.Values["username"]
to the real username then attempting to Response.RedirectToRoute(RouteData); Response.End()
but that always took me to the wrong place (some completely wrong route).
Updated: Thanks to BuildStarted for leading me to this answer:
public class UserAccountController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
if ((string) filterContext.RouteData.Values["username"] != "your")
return;
var routeValues = new RouteValueDictionary(filterContext.RouteData.Values);
routeValues["username"] = UserSession.Current.User.Username;
filterContext.Result = new RedirectToRouteResult(routeValues);
}
}
You can use the FilterAttribute with IActionFilter to accomplish what you want.
public class UserFilterAttribute : FilterAttribute, IActionFilter {
public void OnActionExecuted(ActionExecutedContext filterContext) {
}
public void OnActionExecuting(ActionExecutingContext filterContext) {
var username = filterContext.RouteData.Values["username"];
var realUserName = ""; //load from database
filterContext.Result = new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary(new { controller = "Users", action = "Index", username = realUserName }));
}
}
Then on your ActionResult in your controller you could apply [UserFilter]
to the action.
[UserFilter]
public ActionResult UnknownUserHandler() {
return View();
}
This should get you the results you're looking for. Any questions please post :)
这篇关于我怎样才能更改路由值然后重定向到这条路?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!