asp.net-mvc 在 ASP.NET MVC 中,response.redirect 是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2822562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In ASP.NET MVC, how does response.redirect work?
提问by Swoop
I have used response.redirect in classic ASP and ASP.NET webforms. However, with MVC 2.0, I am running into something peculiar.
我在经典的 ASP 和 ASP.NET webforms 中使用了 response.redirect。但是,使用 MVC 2.0,我遇到了一些奇怪的问题。
I have a private method in a controller class that is used by multiple controller methods to help load and validate some information. This private method is setup to redirect if a problem is discovered to a generic error message page.
我在控制器类中有一个私有方法,多个控制器方法使用它来帮助加载和验证一些信息。如果发现问题,则将此私有方法设置为重定向到通用错误消息页面。
The big problem I am noticing is that the calling controller class and page view attempt to complete rendering and loading before the redirect actually takes place. This is annoying in development because the View throws exceptions that I need to ignore before my generic error page finally loads.
我注意到的一个大问题是调用控制器类和页面视图尝试在重定向实际发生之前完成呈现和加载。这在开发过程中很烦人,因为在最终加载通用错误页面之前,视图抛出了我需要忽略的异常。
As mentioned above, I am used to the older model of response.redirect which prevented subsequent code on a page from being executed as the new page would then load.
如上所述,我习惯了旧的 response.redirect 模型,它阻止页面上的后续代码在新页面加载时被执行。
Any help or advice on redirects in MVC would be greatly appreciated.
任何有关 MVC 重定向的帮助或建议将不胜感激。
回答by JasonTrue
The conventional mechanism to redirect in ASP.Net MVC is to return an object of type RedirectResult to the client. If this is done before your View method is called, your view methods will never be called.
在 ASP.Net MVC 中重定向的常规机制是向客户端返回一个 RedirectResult 类型的对象。如果在调用 View 方法之前完成此操作,则永远不会调用您的视图方法。
If you call Response.Redirect yourself, instead of letting Asp.Net MVC's front controller do that for you, your controller method will continue on until it finishes or throws an exception.
如果您自己调用 Response.Redirect,而不是让 Asp.Net MVC 的前端控制器为您执行此操作,您的控制器方法将继续运行,直到它完成或引发异常。
The idiomatic solution for your problem is to have your private method return a result that your controller can use.
您的问题的惯用解决方案是让您的私有方法返回您的控制器可以使用的结果。
for example:
例如:
public ActionResult Edit(MyEntity entity)
{
if (!IsValid()) return Redirect("/oops/");
...
return View();
}
private bool IsValid()
{
if (nozzle==NozzleState.Closed) return false;
if (!hoseAttached) return false;
return (userRole==Role.Gardener);
}
回答by Robert Harvey
In ASP.NET MVC, you would normally redirect to another page by returning a RedirectResult
from the controller method.
在 ASP.NET MVC 中,您通常会通过RedirectResult
从控制器方法返回 a来重定向到另一个页面。
Example:
例子:
public ActionResult Details(int id)
{
// Attempt to get record from database
var details = dataContext.GetDetails(id);
// Does requested record exist?
if (details == null)
{
// Does not exist - display index so user can choose
return RedirectToAction("Index");
}
// Display details as usual
return View(details);
}
回答by Tom Stickel
In MVC never use Response.Redirect use
在 MVC 中永远不要使用 Response.Redirect 使用
return RedirectToAction("ActionResultName", "ControllerName");
As to WHYto NOT use Response.Redirect in MVC:
至于为什么不在MVC 中使用 Response.Redirect :
- Performance issues with it
- Not following standard asp.net MVC pattern and conventions that have been built specifically FOR MVC.
- 它的性能问题
- 不遵循专门为 MVC 构建的标准 asp.net MVC 模式和约定。
回答by adnan
its recommended and its the only way to redirect to asp.net form in mvc controller action by using
它是推荐的,也是通过使用重定向到 mvc 控制器操作中的 asp.net 表单的唯一方法
return Redirect("/page/pagename.aspx");
other way we can redirect by using ( not recommended and bad way )
我们可以通过使用重定向的其他方式(不推荐和糟糕的方式)
Response.Redirect("/page/pagename.aspx", true);
it will work for redirect, but problem is it will clear all of our Session values. so why its not recommended.
它适用于重定向,但问题是它会清除我们所有的 Session 值。那么为什么不推荐它。
回答by Hitendra
try this code in mvc view page lode
在 mvc 视图页面中尝试此代码
if (Session["UserName"] == null)
{
this.Response.Redirect("LogOn");
}