asp.net-mvc ASP.NET MVC 中的 Redirect 和 RedirectToAction 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12198909/
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
What is the difference between Redirect and RedirectToAction in ASP.NET MVC?
提问by gmail user
What is the difference between Redirectand RedirectToActionother than their return type?
When do we use each? Explanation with any real life scenario would help me greatly.
是什么区别Redirect和RedirectToAction比自己返回类型其他?我们什么时候使用每个?任何现实生活场景的解释都会对我有很大帮助。
I was looking at Confusion between Redirect and RedirectToAction, but, to me, it looks like the answer is more specific towards handling idparameter and returning proper view.
我在看Redirect 和 RedirectToAction 之间的混淆,但是,对我来说,答案似乎更具体地处理id参数和返回正确的视图。
回答by Omar
RedirectToActionlets you construct a redirect url to a specific action/controller in your application, that is, it'll use the route table to generate the correct URL.
RedirectToAction允许您构建一个重定向 url 到应用程序中的特定操作/控制器,也就是说,它将使用路由表生成正确的 URL。
Redirectrequires that you provide a full URL to redirect to.
Redirect要求您提供要重定向到的完整 URL。
If you have an action Indexon controller Homewith parameter Id:
如果您Index对Home带有参数的控制器执行操作Id:
You can use
RedirectToAction("Index", "Home", new { id = 5 })which will generate the URL for you based on your route table.You can use
Redirectbut must construct the URL yourself, so you passRedirect("/Home/Index/5")or however your route table works.You can't redirect to
google.com(an external URL) usingRedirectToAction, you must useRedirect.
您可以使用
RedirectToAction("Index", "Home", new { id = 5 })which 将根据您的路由表为您生成 URL。您可以使用
Redirect但必须自己构建 URL,因此您可以通过Redirect("/Home/Index/5")或路由表正常工作。您不能使用 重定向到
google.com(外部 URL)RedirectToAction,您必须使用Redirect.
RedirectToActionis meant for doing 302 redirects within your application and gives you an easier way to work with your route table.
RedirectToAction用于在您的应用程序中进行 302 重定向,并为您提供一种更简单的方法来处理路由表。
Redirectis meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.
Redirect用于对其他所有内容进行 302 重定向,特别是外部 URL,但您仍然可以在应用程序内重定向,您只需要自己构建 URL。
Best Practices:Use RedirectToActionfor anything dealing with your application actions/controllers. If you use Redirectand provide the URL, you'll need to modify those URLs manually when your route table changes.
最佳实践:使用RedirectToAction任何东西处理您的应用程序操作/控制器。如果您使用Redirect并提供 URL,则需要在路由表更改时手动修改这些 URL。

