Question
In which context, I can use RedirectToAction
and where to use RedirectToRouteResult
?
在哪个上下文中,我可以使用RedirectToAction以及在哪里使用RedirectToRouteResult?
I have two Action Methods like below.
我有两个动作方法,如下所示。
Approach - 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
I could write the same code like below as well. The only difference is that this time I used RedirectToAction
in 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
3 个解决方案
#1
13
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()?
- 使用RedirectToRouteResult()时是否需要永久重定向标志?
- Do I want to write the extra code when using 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 RedirectToRouteResult
is.
这里有一些关于RedirectToRouteResult的信息。
Reference to some MVC Redirects.
参考一些MVC重定向。
In this you will notice that RedirectToRouteResult
is not something that you would normally call to return in an action. It is used as a return type for multiple RedirectToRoute
calls. For instance, there are 2 calls you will see in that book. RedirectToRoute
and RedirectToRoutePermanent
.
在这里你会注意到RedirectToRouteResult不是你通常会在动作中调用的东西。它用作多个RedirectToRoute调用的返回类型。例如,您将在该书中看到2个电话。 RedirectToRoute和RedirectToRoutePermanent。
They both return RedirectToRouteResult
except, RedirectToRoutePermanent
returns the result with the permanent redirect bool true
. This returns a HTTP 301 status code
.
它们都返回RedirectToRouteResult,RedirectToRoutePermanent返回结果,永久重定向bool为true。这将返回HTTP 301状态代码。
Hope this helps!
希望这可以帮助!
#2
3
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" }));
}
}
#3
0
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:
您可以在助手中找到这种选项,例如,新闻纸网站:
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
并重定向它是一样的
#1
13
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()?
- 使用RedirectToRouteResult()时是否需要永久重定向标志?
- Do I want to write the extra code when using 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 RedirectToRouteResult
is.
这里有一些关于RedirectToRouteResult的信息。
Reference to some MVC Redirects.
参考一些MVC重定向。
In this you will notice that RedirectToRouteResult
is not something that you would normally call to return in an action. It is used as a return type for multiple RedirectToRoute
calls. For instance, there are 2 calls you will see in that book. RedirectToRoute
and RedirectToRoutePermanent
.
在这里你会注意到RedirectToRouteResult不是你通常会在动作中调用的东西。它用作多个RedirectToRoute调用的返回类型。例如,您将在该书中看到2个电话。 RedirectToRoute和RedirectToRoutePermanent。
They both return RedirectToRouteResult
except, RedirectToRoutePermanent
returns the result with the permanent redirect bool true
. This returns a HTTP 301 status code
.
它们都返回RedirectToRouteResult,RedirectToRoutePermanent返回结果,永久重定向bool为true。这将返回HTTP 301状态代码。
Hope this helps!
希望这可以帮助!
#2
3
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" }));
}
}
#3
0
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:
您可以在助手中找到这种选项,例如,新闻纸网站:
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
并重定向它是一样的