Using set instead of add in Created ActionResults

This commit is contained in:
Ajay Bhargav Baaskaran 2015-01-15 18:47:02 -08:00
parent 7c0eb56e59
commit b02dea98e7
4 changed files with 21 additions and 3 deletions

View File

@ -71,7 +71,7 @@ namespace Microsoft.AspNet.Mvc
throw new InvalidOperationException(Resources.NoRoutesMatched);
}
context.HttpContext.Response.Headers.Add("Location", new string[] { url });
context.HttpContext.Response.Headers.Set("Location", url);
}
}
}

View File

@ -69,7 +69,7 @@ namespace Microsoft.AspNet.Mvc
throw new InvalidOperationException(Resources.NoRoutesMatched);
}
context.HttpContext.Response.Headers.Add("Location", new string[] { url });
context.HttpContext.Response.Headers.Set("Location", url);
}
}
}

View File

@ -31,7 +31,7 @@ namespace Microsoft.AspNet.Mvc
/// <inheritdoc />
protected override void OnFormatting([NotNull] ActionContext context)
{
context.HttpContext.Response.Headers.Add("Location", new string[] { Location });
context.HttpContext.Response.Headers.Set("Location", Location);
}
}
}

View File

@ -45,6 +45,24 @@ namespace Microsoft.AspNet.Mvc
Assert.Equal(location, httpContext.Response.Headers["Location"]);
}
[Fact]
public async Task CreatedResult_OverwritesLocationHeader()
{
// Arrange
var location = "/test/";
var httpContext = GetHttpContext();
var actionContext = GetActionContext(httpContext);
httpContext.Response.Headers.Set("Location", "/different/location/");
var result = new CreatedResult(location, "testInput");
// Act
await result.ExecuteResultAsync(actionContext);
// Assert
Assert.Equal(201, httpContext.Response.StatusCode);
Assert.Equal(location, httpContext.Response.Headers["Location"]);
}
private static ActionContext GetActionContext(HttpContext httpContext)
{
var routeData = new RouteData();