POST メソッドの場合、W3 仕様は次のように述べています。
リソースがオリジンサーバーで作成されている場合、応答は 201 (Created) であり、要求のステータスを記述し、新しいリソースを参照するエンティティと、Location ヘッダーを含む必要があります (セクション 10.4 を参照)。
http://www.ietf.org/internet-drafts/draft-ietf-httpbis-p2-semantics-05.txt (セクション 8.5)
実際の標準的な応答は、新しく作成されたリソースにリダイレクトを送信することのようです。
ASP.NET MVC を使用してサイトを構築しており、仕様に従おうとしたため、ResourceCreatedResult
クラスを作成しました。
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);
}
}
そして、私のアクションは次のようになります。
[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 はすべて、新しいリソースへのリダイレクトに失敗します。正しい応答を生成するのに失敗したのでしょうか、それとも Web ブラウザーがこのタイプの応答を予期せず、代わりにサーバーに依存してリダイレクト応答を送信するのでしょうか?