什么是一个HTTP POST请求正确的响应? [英] What is the correct response to an HTTP POST request?

查看:184
本文介绍了什么是一个HTTP POST请求正确的响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关POST方法,W3的规格说:

For a POST method, the W3 specs say:

如果一个资源已经在源服务器上创建,响应
     应该是201(创建),并包含它描述了一个实体
     请求的状态,指的是新的资源,和一个位置
     头(见第10.4节)。

If a resource has been created on the origin server, the response SHOULD be 201 (Created) and contain an entity which describes the status of the request and refers to the new resource, and a Location header (see Section 10.4).

<一个href=\"http://www.ietf.org/internet-drafts/draft-ietf-httpbis-p2-semantics-05.txt\">http://www.ietf.org/internet-drafts/draft-ietf-httpbis-p2-semantics-05.txt (8.5节)

的标准响应,实际上似乎是一个重定向发送到新创建的资源。

The standard response actually seems to be to send a Redirect to the newly created resource.

我建设我的网站在ASP.NET MVC,并试图按照规范,因此创造了一个 ResourceCreatedResult 类:

I'm building my site with ASP.NET MVC, and tried to follow the spec, so created a ResourceCreatedResult class:

public class ResourceCreatedResult : ActionResult
{
    public string Location { get; set; }
    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.Clear();
        context.HttpContext.Response.StatusCode = 201;
        context.HttpContext.Response.ClearHeaders();
        context.HttpContext.Response.AddHeader("Location", Location);
    }
}

和我的行动看起来是这样的:

And my action looks something like this:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateNew(string entityStuff)
{
    Entity newEntity = new Entity(entityStuff);
    IEntityRepository entityRepository = ObjectFactory.GetInstance<IEntityRepository>();
    entityRepository.Add(newEntity);

    ActionResult result = new ResourceCreatedResult()
        { Location = Url.Action("Show", new { id = newEntity.Id }) };
    return result;
}

不过,IE,Firefox和Chrome都失败重定向到新的资源。有我搞砸生成正确的响应,还是网页浏览器不能指望这种反应,而不是依赖于服务器发送重定向响应?

However, IE, Firefox and Chrome all fail to redirect to the new resource. Have I messed up generating the correct response, or do web browsers not expect this type of response, instead relying on servers to send a Redirect response?

推荐答案

POST后重定向或交/重定向/ get是什么应用程序必须做的是用户友好的。

Redirect after post or post/redirect/get is something your application must do to be user friendly.

修改。这是超出HTTP规范。如果我们只是一个POST之后返回201,浏览器后退按钮行为恶劣。

Edit. This is above and beyond the HTTP specifications. If we simply return a 201 after a POST, the browser back button behaves badly.

注意,Web服务请求(不给浏览器响应)遵循标准完全和岗位后不要重定向。

Note that Web Services requests (which do NOT respond to a browser) follow the standard completely and do NOT redirect after post.

它的工作原理是这样的。

It works like this.


  1. 浏览器职位的数据。

  1. The browser POSTS the data.

您的应用程序对数据进行验证。如果它是无效的,你的回应的形式,使他们能够修复它和POST。

Your application validates the data. If it's invalid, you respond with the form so they can fix it and POST.

您的应用程序使用重定向响应。

Your application responds with a redirect.

浏览器获得重定向并做了GET。

The browser gets the redirect and does a GET.

您的应用程序看到GET和响应。

Your application sees the GET and responds.

现在 - 嘿preSTO! - 后退按钮工程

Now -- hey presto! -- the back button works.

这篇关于什么是一个HTTP POST请求正确的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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