【发布时间】:2017-04-18 08:25:35
【问题描述】:
知道RedirectToAction,我一直在寻找类似的东西来保持用户的URL稳定,并且仍然将责任从一个动作转移到另一个动作。
既然在这个话题上找到zero Google results,还不如彻底解决一个XY problem。
不过,我将尝试解释为什么我认为可能需要这样做。
场景:
public ActionResult A(int id)
{
var uniqueID = CalculateUniqueFromId(id);
// This compiles.
return RedirectToAction("B", new { uniqueID });
// This does not compile.
return RewriteToAction("B", new { uniqueID });
}
public ActionResult B(Guid uniqueID)
{
var model = DoCreateModelForUnique(uniqueID);
return View("B", model);
}
在上面的代码中,动作 A 从一个整数计算一个 guid 并将其传递给另一个动作。
解决方法:
我可以把上面的代码改成这样:
public ActionResult A(int id)
{
var uniqueID = CalculateUniqueFromId(id);
var model = DoCreateModelForUnique(uniqueID);
return View("B", model);
}
public ActionResult B(Guid uniqueID)
{
var model = DoCreateModelForUnique(uniqueID);
return View("B", model);
}
这将按预期工作。
不过,在更复杂的场景中,我希望时不时使用“服务器端重定向”(也称为重写)。
替代解决方法:
我也可以使用HttpContext.RewritePath,例如在 Global.asax 的Application_BeginRequest 中进行重写。
这让我感觉“脱离了 MVC 上下文”,尽管它按预期工作。
我的问题:
有没有像RewriteToAction 这样优雅的、与MVC 集成的方式?
更新 1:
Luke commented一个有希望的链接:
我会尝试调查,这是否符合我的需要。
【问题讨论】:
-
我认为这不能满足您的要求? stackoverflow.com/a/1180744/894792 或 stackoverflow.com/a/1242525/894792
-
看起来很有希望,@Luke,我去看看,非常感谢????。
-
祝我的朋友好运! :o)
标签: c# asp.net .net asp.net-mvc asp.net-mvc-4