是什么在ASP.NET MVC重定向和RedirectToAction之间的区别? [英] What is the difference between Redirect and RedirectToAction in ASP.NET MVC?

查看:211
本文介绍了是什么在ASP.NET MVC重定向和RedirectToAction之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是重定向之间的区别 RedirectToAction 比他们的返回类型其他?
当我们使用的呢?与任何真实的生活场景说明会对我们帮助很大。

What is the difference between Redirect and RedirectToAction other than their return type? When do we use each? Explanation with any real life scenario would help me greatly.

我在看<一个href=\"http://stackoverflow.com/questions/6477253/confusion-between-redirect-and-redirecttoaction\">Confusion重定向和RedirectToAction ,但为了我之间,它看起来像答案是对处理 ID 参数和返回正确的观点更具体。

I was looking at Confusion between Redirect and RedirectToAction, but, to me, it looks like the answer is more specific towards handling id parameter and returning proper view.

推荐答案

RedirectToAction ,您可以构建一个重定向URL到一个特定的动作/控制器在你的应用程序,也就是说,它会使用路由表来生成正确的URL。

RedirectToAction lets 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.

重定向要求您提供一个完整的URL重定向到。

Redirect requires that you provide a full URL to redirect to.

如果您对控制器的动作首页 首页带参数标识

If you have an action Index on controller Home with parameter Id:


  1. 您可以使用 RedirectToAction(指数,家,新的{n = 5})这将根据您的路线上为您生成URL表。

  1. You can use RedirectToAction("Index", "Home", new { id = 5 }) which will generate the URL for you based on your route table.

您可以使用重定向,但必须自己构造URL,让你通过重定向(/首页/索引/ 5) 或你不过路由表的工作原理。

You can use Redirect but must construct the URL yourself, so you pass Redirect("/Home/Index/5") or however your route table works.

您不能重定向到 google.com 使用(外部URL) RedirectToAction ,你必须使用重定向

You can't redirect to google.com (an external URL) using RedirectToAction, you must use Redirect.

RedirectToAction 是为做302重定向你的应用程序中,并为您提供了一个更简单的方式与您的路由表来工作。

RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table.

重定向是为做302重定向到一切,特别是外部URL,但你仍然可以在你的应用程序中的重定向,你只需要构建自己的URL。

Redirect is 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.

最佳实践:使用 RedirectToAction 任何东西处理您的应用程序操作/控制器。如果你使用重定向并提供网址,你需要手动修改这些URL当你的路由表的更改。

Best Practices: Use RedirectToAction for anything dealing with your application actions/controllers. If you use Redirect and provide the URL, you'll need to modify those URLs manually when your route table changes.

这篇关于是什么在ASP.NET MVC重定向和RedirectToAction之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆