asp.net-mvc 何时使用 RedirectToAction 以及在何处使用 RedirectToRouteResult?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16945519/
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
When to use RedirectToAction and where to use RedirectToRouteResult?
提问by evevffv
Question
题
In which context, I can use RedirectToActionand where to use RedirectToRouteResult?
在哪种情况下,我可以使用RedirectToAction以及在哪里使用RedirectToRouteResult?
I have two Action Methods like below.
我有两个像下面这样的动作方法。
Approach - 1
方法 - 1
public class ActionResultTypesController : Controller
{
public ActionResult Content()
{
return new RedirectToRouteResult(new RouteValueDictionary(
new { action = "Fileresult", controller = "ActionResultTypes" }));
}
public ActionResult Fileresult()
{
return View();
}
}
Approach - 2
方法 - 2
I could write the same code like below as well. The only difference is that this time I used RedirectToActionin place of RedirectToRouteResult
我也可以编写与下面相同的代码。唯一不同的是,这次我用RedirectToAction了代替RedirectToRouteResult
public class ActionResultTypesController : Controller
{
public ActionResult Content()
{
return RedirectToAction("Fileresult", "ActionResultTypes");
}
public ActionResult Fileresult()
{
return View();
}
}
Both piece of code have common Resultant
两段代码都有共同的结果
采纳答案by technicallyjosh
There isn't much difference between the two when using within the controller like you have in your example.
像您在示例中那样在控制器中使用时,两者之间没有太大区别。
They both ultimately achieve the same goal. However, RedirectToRouteResult() is mostly used in an action filter type scenario seen here.It's a little less friendly on the eyes when just using in your actions on controllers.
他们最终都实现了相同的目标。但是, RedirectToRouteResult() 主要用于此处看到的操作过滤器类型场景。仅在控制器上的操作中使用时,它对眼睛的友好度会有所降低。
Both can achieve the same goal. The questions you need to ask yourself in most scenarios are really:
两者都可以实现相同的目标。在大多数情况下,您需要问自己的问题实际上是:
- Do I need the permanent redirect flag when using RedirectToRouteResult()?
- Do I want to write the extra code when using RedirectToRouteResult()?
- 使用 RedirectToRouteResult() 时是否需要永久重定向标志?
- 我想在使用 RedirectToRouteResult() 时编写额外的代码吗?
If your answer is no or I don't know,
如果你的答案是否定的或者我不知道,
RedirectToAction("Action", "Controller", new { parameter = value });
is probably your best bet!
可能是你最好的选择!
EDIT:
编辑:
Here's some light on what RedirectToRouteResultis.
这里有一些关于什么RedirectToRouteResult是的光。
Reference to some MVC Redirects.
In this you will notice that RedirectToRouteResultis not something that you would normally call to return in an action. It is used as a return type for multiple RedirectToRoutecalls. For instance, there are 2 calls you will see in that book. RedirectToRouteand RedirectToRoutePermanent.
在这里你会注意到这RedirectToRouteResult不是你通常会在一个动作中调用返回的东西。它用作多个RedirectToRoute调用的返回类型。例如,您将在那本书中看到 2 个调用。RedirectToRoute和RedirectToRoutePermanent。
They both return RedirectToRouteResultexcept, RedirectToRoutePermanentreturns the result with the permanent redirect bool true. This returns a HTTP 301 status code.
它们都返回RedirectToRouteResultexcept,RedirectToRoutePermanent返回带有永久重定向 bool 的结果true。这将返回一个HTTP 301 status code.
Hope this helps!
希望这可以帮助!
回答by Dawie Snyman
I am new to MVC but have found that I am using a custom class Authenticate and one of the properties returns a RedirectToRouteResult. This class is not a controller (or derived from it) so RedirectToAction is not available and I would use RedirectToRouteResult.
我是 MVC 的新手,但发现我使用的是自定义类 Authenticate,其中一个属性返回 RedirectToRouteResult。此类不是控制器(或派生自控制器),因此 RedirectToAction 不可用,我将使用 RedirectToRouteResult。
The Property looks like this:
该属性如下所示:
public RedirectToRouteResult NotLoggedInPage
{
get
{
return new RedirectToRouteResult(new RouteValueDictionary(new { action = "LoggedOut", controller = "Login" }));
}
}
回答by Zach dev
It's almost the same but...
what happens when you work with a few custom routes? is an alternative that supports routes
几乎相同,但是...
当您使用一些自定义路由时会发生什么?是支持路由的替代方案
with the second approach you work with the default route, but when you need use a specific route with 3 or 4 parameters, you can use the first approach and specified route name and with all parameters.
对于第二种方法,您使用默认路由,但是当您需要使用具有 3 或 4 个参数的特定路由时,您可以使用第一种方法和指定的路由名称以及所有参数。
this kind of options you can find in helpers too, for example, a news paper site:
您也可以在 helpers 中找到此类选项,例如,新闻报纸网站:
your project has two routes
你的项目有两条路线
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"NewsRoute", // Route name
"News/{action}/{year}/{month}/{day}/{news}",
new { controller = "News", action = "show", year = 2013, month = 6, news = "start-new" }
);
the default route it's used for contents and special pages and the NewsRoute it's used for show and edit news
用于内容和特殊页面的默认路由以及用于显示和编辑新闻的 NewsRoute
if you need build some url you must do it like this
如果你需要建立一些网址,你必须这样做
@Url.Action("Home","Contact")
@Url.RouteUrl("NewsRoute", new RouteValueDictionary(new {action = "show", year = 2013, month = 6, news = "title news" }));
and the redirect it's the same way
和重定向也是一样的

