问题描述
问
在这方面,我可以使用 RedirectToAction
何地使用 RedirectToRouteResult
?
我有两个动作方法如下图所示。
法 - 1
公共类ActionResultTypesController:控制器
{
公众的ActionResult内容()
{
返回新RedirectToRouteResult(新RouteValueDictionary(
新{行动=Fileresult,控制器=ActionResultTypes}));
}
公众的ActionResult Fileresult()
{
返回查看();
}
}
法 - 2
我可以写在同一code像下面为好。唯一不同的是,这一次我在 RedirectToRouteResult
RedirectToAction
公共类ActionResultTypesController:控制器
{
公众的ActionResult内容()
{
返回RedirectToAction(Fileresult,ActionResultTypes);
}
公众的ActionResult Fileresult()
{
返回查看();
}
}
两个片code有共同的合力
在控制器内使用时,就像你在你的例子有没有两者太大的差别。
他们都最终达到同样的目标。然而,RedirectToRouteResult()在动作滤波器类型的场景seen这里它的眼睛有点不太友好只是用你的行动上的控制器时。
两者都可以达到同样的目标。你需要问自己在大多数情况下的问题是真的:
- 请我用RedirectToRouteResult时需要的永久重定向标志()?
- 请我想用RedirectToRouteResult时写的额外的code()?
如果你的答案是没有或不知道,
RedirectToAction(行动,控制器,新的{参数=值});
可能是你最好的选择!
编辑:
下面是一些什么样的光 RedirectToRouteResult
是
<一个href=\"http://books.google.com/books?id=ZscWT8HzDVAC&pg=PT494&lpg=PT494&dq=RedirectToRouteResult.Permanent?&source=bl&ots=GrFt5tRIn0&sig=j_z_oH_GoB9_PDUGT4teVUYL-ag&hl=en&sa=X&ei=qaawUcfMLMa9yAHcyoGYCQ&ved=0CFkQ6AEwBQ\">Reference一些MVC重定向。
在此,你会发现 RedirectToRouteResult
是不是你通常会调用的行动来回报。它是用来作为返回类型为多个 RedirectToRoute
来电。举例来说,有2个电话,你将在本书中看到。 RedirectToRoute
和 RedirectToRoutePermanent
。
他们都返回 RedirectToRouteResult
除外, RedirectToRoutePermanent
与永久重定向布尔返回结果真正
。这会返回一个 HTTP 301状态code
。
希望这有助于!
Question
In which context, I can use RedirectToAction
and where to use 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
public class ActionResultTypesController : Controller
{
public ActionResult Content()
{
return RedirectToAction("Fileresult", "ActionResultTypes");
}
public ActionResult Fileresult()
{
return View();
}
}
Both piece of code have common Resultant
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.
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()?
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.
Reference to some MVC Redirects.
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
.
They both return RedirectToRouteResult
except, RedirectToRoutePermanent
returns the result with the permanent redirect bool true
. This returns a HTTP 301 status code
.
Hope this helps!
这篇关于何时使用RedirectToAction何地使用RedirectToRouteResult?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!