的巴尔德的解决方案工作,但不是最佳的。
让我们举个例子:
public class HomeController : Controller
{
protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
{
Response.Redirect("http://www.google.com");
return base.BeginExecuteCore(callback, state);
}
// GET: Test
public ActionResult Index()
{
// Put a breakpoint under this line
return View();
}
}
如果运行这个项目,你会明显得到了谷歌主页。但是如果你看看你的IDE,你会注意到由于断点,代码正在等待你。 为什么?因为你重定向了响应,但并没有停止ASP.NET MVC的流动,所以它继续这个过程(通过调用动作)。
这不是一个大问题,一个小网站,但如果你预计将有大量的游客,这可以成为一个严重性能问题:因为反应已经消失了每秒运行的请求没有任何的潜在千元。
你怎么能避免这种情况?我有一个解决方案(不是一个漂亮的一个,但它的工作):
public class HomeController : Controller
{
public ActionResult BeginExecuteCoreActionResult { get; set; }
protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
{
this.BeginExecuteCoreActionResult = this.Redirect("http://www.google.com");
// or : this.BeginExecuteCoreActionResult = new RedirectResult("http://www.google.com");
return base.BeginExecuteCore(callback, state);
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Result = this.BeginExecuteCoreActionResult;
base.OnActionExecuting(filterContext);
}
// GET: Test
public ActionResult Index()
{
// Put a breakpoint under this line
return View();
}
}
您存储控制器部件内部的重定向结果和OnActionExecuting运行时,你执行吧!
虽然您的问题缺乏细节,但您可以阅读有关Controller.RedirectToAction方法。 – Balde