重定向和RedirectToAction之间的混淆 [英] Confusion between Redirect and RedirectToAction

查看:1238
本文介绍了重定向和RedirectToAction之间的混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习了MS证书(70-515)。结果
我是一个困惑与我在网上找到的,也是我一个实践的检验读取。结果
在这么几个问题,指出使用RedirectToAction发送浏览器中的302,从而使其改变它在地址栏中的URL。

但是,这是从实践中检验的1个问题:

问:


  

在MVC Home控制器目前只有默认的指数操作。有关code是表现在以下code的例子。


 公众的ActionResult指数()
{
    计算机[消息] =你好!
    返回查看();
}


  

您需要创建一个名为FindID显示输入为路径的一部分ID参数的操作。如果路径不包括ID参数,ASP.NET必须没有在浏览器的地址栏更改URL处理Index操作,也不能抛出异常。
  其中code段你应该使用?


正确答案:

 公众的ActionResult FindID(INT?ID)
{
    如果(!id.HasValue)
        返回RedirectToAction(「指数」);
    计算机[消息] =ID为+ id.ToString();
    返回查看();
}

说明:


  

您可以使用的ActionResult的RedirectToAction形式造成MVC从一个动作中处理不同的操作。 MVC放弃当前操作并处理该请求,如果路线已经直接导致您重定向到的动作。从本质上讲,这相当于在一个标准的ASP.NET应用程序中调用Server.Transfer的。


  
  

在重定向的ActionResult发送一个HTTP错误302 - 找到响应浏览器,这会导致浏览器加载指定的URL。这就改变显示在地址栏中的地址。


所以:结果
- 做了RedirectToAction离开URL浏览器原封不动结果?
- 是否重定向更改URL浏览器结果?
- 是实践检验的正确的解释?从我的理解是RedirectToAction没有做一个302


解决方案

  

您可以使用的ActionResult的RedirectToAction形式造成MVC从一个动作中处理不同的操作。 MVC放弃当前操作并处理该请求,如果路线已经直接导致您重定向到的动作。从本质上讲,这相当于在一个标准的ASP.NET应用程序中调用Server.Transfer的。


这是不正确。

无论是RedirectToRouteResult(RedirectToAction)和RedirectResult执行302重定向,从而导致在浏览器更改URL。

要返回索引结果不改变URL中的code实际上是:

 公众的ActionResult FindID(INT?ID)
{
    如果(!id.HasValue)
        返回视图(指数);
    计算机[消息] =ID为+ id.ToString();
    返回查看();
}

不过,我并不推荐这种方法。如果我做出 mysite.com/products/some-product 部分产品不存在一个请求,然后我应该通知用户相关的状态code(搜索引擎也很重要)。

如果您FindID行动的唯一目的是做的id参数的东西,那么它不应该可空/可选的。这样,如果没有指定一个ID是在FindID行动不会被调用。

I'm studying for a MS certificate (70-515).
I'm a confused with what I find online and what I read in a practice test.
A few questions on SO state that using a RedirectToAction is sending the browser a 302, thus causing it to change it's url in the address bar.

But this is a question from 1 of the practice tests:

QUESTION:

The MVC Home controller currently has only the default Index action. The relevant code is shown in the following code example.

public ActionResult Index()
{
    ViewData["Message"] = "Hello!";
    return View();
}

You need to create an action named FindID that displays the ID parameter entered as part of the path. If the path does not include an ID parameter, ASP.NET must process the Index action without changing the URL in the browser's address bar, and must not throw an exception. Which code segment should you use?

CORRECT ANSWER:

public ActionResult FindID(int? id)
{
    if (!id.HasValue)
        return RedirectToAction("Index");
    ViewData["Message"] = "ID is " + id.ToString();
    return View();
}

EXPLANATION:

You can use the RedirectToAction form of ActionResult to cause MVC to process a different action from within an action. MVC abandons the current action and processes the request as if the route had led directly to the action you redirect to. Essentially, this is equivalent to calling Server.Transfer in a standard ASP.NET application.

The Redirect ActionResult sends an "HTTP Error 302 - Found" response to the browser, which causes the browser to load the specified URL. This changes the address that appears in the address bar.

So:
- Does a RedirectToAction leave the URL in browser untouched?
- Does a Redirect change the URL in browser?
- Is the explanation of the practice test correct? From that I understand that RedirectToAction does NOT do a 302.

解决方案

You can use the RedirectToAction form of ActionResult to cause MVC to process a different action from within an action. MVC abandons the current action and processes the request as if the route had led directly to the action you redirect to. Essentially, this is equivalent to calling Server.Transfer in a standard ASP.NET application.

This is incorrect.

Both the RedirectToRouteResult (RedirectToAction) and RedirectResult perform a 302 redirect, resulting in the URL in the browser changing.

To return the Index result without changing the url the code would actually be:

public ActionResult FindID(int? id)
{
    if (!id.HasValue)
        return View("index");
    ViewData["Message"] = "ID is " + id.ToString();
    return View();
}

However, I would not recommended this approach. If I make a request to mysite.com/products/some-product and some-product does not exist, then I should inform the user of that with the relevant status code (also important for search engines).

If the sole purpose of your FindID action is to do something with the id parameter, then it should not be nullable/optional. This way the FindID action would not be invoked if an ID was not specified.

这篇关于重定向和RedirectToAction之间的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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