asp.net-mvc – 何时使用RedirectToAction和哪里使用RedirectToR
题
在哪个上下文中,我可以使用RedirectToAction和哪里使用RedirectToRouteResult? 我有两个动作方法,如下所示。 方法 – 1 public class ActionResultTypesController : Controller { public ActionResult Content() { return new RedirectToRouteResult(new RouteValueDictionary( new { action = "Fileresult",controller = "ActionResultTypes" })); } public ActionResult Fileresult() { return View(); } } 方法 – 2 我也可以编写如下所示的相同的代码。唯一的区别是这次我使用RedirectToAction代替RedirectToRouteResult public class ActionResultTypesController : Controller { public ActionResult Content() { return RedirectToAction("Fileresult","ActionResultTypes"); } public ActionResult Fileresult() { return View(); } } 两段代码都具有共同的结果 解决方法
在控制器中使用时,两者之间没有太大的区别,就像你在你的例子中一样。
他们最终都达到了同样的目标。但是,RedirectToRouteResult()主要用于操作过滤器类型场景seen here.当您在控制器上的操作中使用时,眼睛稍微不那么友善。 两者都能实现同样的目标。在大多数情况下,您需要问的问题是: >使用RedirectToRouteResult()时,是否需要永久重定向标志? 如果你的答案是否定的或者我不知道, RedirectToAction("Action","Controller",new { parameter = value }); 可能是你最好的打赌! 编辑: 这里有一些关于RedirectToRouteResult的内容。 Reference to some MVC Redirects. 在这里你会注意到,RedirectToRouteResult不是你通常在一个操作中返回的东西。它被用作多个RedirectToRoute调用的返回类型。例如,您将在该书中看到2个电话。 RedirectToRoute和RedirectToRoutePermanent。 他们都返回RedirectToRouteResult,除了RedirectToRoutePermanent使用永久重定向bool true返回结果。这将返回HTTP 301状态代码。 希望这可以帮助! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 处理asp.net core EF model 中有自增列的问题
- 使用可靠的WPF / .NET背景学习ASP.NET MVC
- asp.net-mvc – 在ASP.NET MVC项目中访问elmah.axd时出现“
- asp.net – 如何防止Hangfire重复作业在连续执行30分钟后重
- 如何使用asp.net获取html select的所选值
- ASP.NET身份两个因素不起作用 – Cookie问题?
- asp.net-mvc – ASP.NET MVC – 从单个控制器动作返回不同的
- asp.net – 强制按钮控制在回发之前运行其客户端
- asp.net-mvc – 如何将XML作为POST传递给ASP MVC .NET中的A
- asp.net-mvc – 认证之前调用的ASP.NET MVC控制器构造函数