Skip to content

Commit b14a7ca

Browse files
Code Quality: Fix ASP0019 warnings by replacing Headers.Add with Headers.Append (#21260)
Use Append for response headers - Replaces Add with Append when adding response headers to conform to IHeaderDictionary usage. - Applies across unauthorized handling, redirects, and custom header signaling. - Updates tests to use Append consistently. - Removes ASP0019 from warnings in project configs to reflect new approach.
1 parent 0803ab8 commit b14a7ca

File tree

10 files changed

+13
-15
lines changed

10 files changed

+13
-15
lines changed

src/Umbraco.Cms.Api.Delivery/Controllers/Content/ByRouteContentApiController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ private async Task<IActionResult> HandleRequest(string path)
9999

100100
private IActionResult RedirectTo(IApiContentRoute redirectRoute)
101101
{
102-
Response.Headers.Add("Location-Start-Item-Path", redirectRoute.StartItem.Path);
103-
Response.Headers.Add("Location-Start-Item-Id", redirectRoute.StartItem.Id.ToString("D"));
102+
Response.Headers.Append("Location-Start-Item-Path", redirectRoute.StartItem.Path);
103+
Response.Headers.Append("Location-Start-Item-Id", redirectRoute.StartItem.Id.ToString("D"));
104104
return RedirectPermanent(redirectRoute.Path);
105105
}
106106
}

src/Umbraco.Cms.Api.Delivery/Umbraco.Cms.Api.Delivery.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
<PropertyGroup>
88
<!--
99
TODO: Fix and remove overrides:
10-
[ASP0019] use IHeaderDictionary.Append or the indexer to append or set headers
1110
[CS0618/CS0612] update obsolete references
1211
-->
13-
<WarningsNotAsErrors>$(WarningsNotAsErrors),ASP0019,CS0618,CS0612</WarningsNotAsErrors>
12+
<WarningsNotAsErrors>$(WarningsNotAsErrors),CS0618,CS0612</WarningsNotAsErrors>
1413
</PropertyGroup>
1514

1615
<ItemGroup>

src/Umbraco.Web.Common/Filters/ModelBindingExceptionAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Net;
22
using System.Text.RegularExpressions;
3+
using Microsoft.AspNetCore.Http;
34
using Microsoft.AspNetCore.Http.Extensions;
45
using Microsoft.AspNetCore.Mvc;
56
using Microsoft.AspNetCore.Mvc.Filters;
@@ -56,7 +57,7 @@ public void OnException(ExceptionContext filterContext)
5657
&& (filterContext.Exception is ModelBindingException || filterContext.Exception is InvalidCastException)
5758
&& IsMessageAboutTheSameModelType(filterContext.Exception.Message))
5859
{
59-
filterContext.HttpContext.Response.Headers.Add(HttpResponseHeader.RetryAfter.ToString(), "1");
60+
filterContext.HttpContext.Response.Headers.Append(HttpResponseHeader.RetryAfter.ToString(), "1");
6061
filterContext.Result = new RedirectResult(filterContext.HttpContext.Request.GetEncodedUrl(), false);
6162

6263
filterContext.ExceptionHandled = true;

src/Umbraco.Web.Common/Filters/UmbracoUserTimeoutFilterAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Globalization;
2+
using Microsoft.AspNetCore.Http;
23
using Microsoft.AspNetCore.Mvc;
34
using Microsoft.AspNetCore.Mvc.Filters;
45
using Umbraco.Extensions;
@@ -28,7 +29,7 @@ public void OnActionExecuted(ActionExecutedContext context)
2829
}
2930

3031
var remainingSeconds = context.HttpContext.User.GetRemainingAuthSeconds();
31-
context.HttpContext.Response.Headers.Add(
32+
context.HttpContext.Response.Headers.Append(
3233
"X-Umb-User-Seconds",
3334
remainingSeconds.ToString(CultureInfo.InvariantCulture));
3435
}

src/Umbraco.Web.Common/Umbraco.Web.Common.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
[SA1117] params all on same line
1313
[SA1401] make fields private
1414
[SA1134] own line attributes
15-
[ASP0019] use IHeaderDictionary.Append or the indexer to append or set headers
1615
[CS0618]/[SYSLIB0051] adjust obsolete references
1716
[IDE0040]/[SA1400] access modifiers
1817
[SA1405] Debug assert message text
1918
[CS0419]/[CS1574] cref ambiguities
2019
[SA1649] file name match type
2120
-->
22-
<WarningsNotAsErrors>$(WarningsNotAsErrors),SA1117,SA1401,SA1134,ASP0019,CS0618,SYSLIB0051,IDE0040,SA1400,SA1405,CS0419,CS1574,SA1649</WarningsNotAsErrors>
21+
<WarningsNotAsErrors>$(WarningsNotAsErrors),SA1117,SA1401,SA1134,CS0618,SYSLIB0051,IDE0040,SA1400,SA1405,CS0419,CS1574,SA1649</WarningsNotAsErrors>
2322
</PropertyGroup>
2423

2524
<ItemGroup>

src/Umbraco.Web.Website/Middleware/BasicAuthenticationMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private void HandleUnauthorized(HttpContext context)
106106
else
107107
{
108108
context.Response.StatusCode = 401;
109-
context.Response.Headers.Add("WWW-Authenticate", "Basic realm=\"Umbraco login\"");
109+
context.Response.Headers.Append("WWW-Authenticate", "Basic realm=\"Umbraco login\"");
110110
}
111111
}
112112
}

src/Umbraco.Web.Website/Umbraco.Web.Website.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
<PropertyGroup>
1010
<!--
1111
TODO: Fix and remove overrides:
12-
[ASP0019] use IHeaderDictionary.Append or the indexer to append or set headers
1312
[CS0618] handle member obsolete appropriately
1413
[SA1401] make fields private
1514
[SA1649] update file name
1615
[IDE1006] fix naming rule violation
1716
-->
18-
<WarningsNotAsErrors>$(WarningsNotAsErrors),ASP0019,CS0618,SA1401,SA1649,IDE1006</WarningsNotAsErrors>
17+
<WarningsNotAsErrors>$(WarningsNotAsErrors),CS0618,SA1401,SA1649,IDE1006</WarningsNotAsErrors>
1918
</PropertyGroup>
2019

2120
<ItemGroup>

tests/Umbraco.Tests.UnitTests/Umbraco.Tests.UnitTests.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@
1717
[SA1401] fields must be private
1818
[SA1405] debug message text
1919
[IDE0060] remove parameter
20-
[ASP0019] header append
2120
[CS0114] inherited member
2221
[CS0661]/[CS0659] adjust overrides
2322
[CS0414] unassigned field
2423
[CS0252] confirm reference comparison
2524
[CS0612] obsolete
2625
[IDE1006] fix naming rule violation
2726
-->
28-
<WarningsNotAsErrors>$(WarningsNotAsErrors),SYSLIB0013,CS0618,CS1998,SA1117,CS0067,CA1822,CA1416,IDE0028,SA1401,SA1405,IDE0060,ASP0019,CS0114,CS0661,CS0659,CS0414,CS0252,CS0612,IDE1006</WarningsNotAsErrors>
27+
<WarningsNotAsErrors>$(WarningsNotAsErrors),SYSLIB0013,CS0618,CS1998,SA1117,CS0067,CA1822,CA1416,IDE0028,SA1401,SA1405,IDE0060,CS0114,CS0661,CS0659,CS0414,CS0252,CS0612,IDE1006</WarningsNotAsErrors>
2928
</PropertyGroup>
3029

3130
<ItemGroup>

tests/Umbraco.Tests.UnitTests/Umbraco.Web.Common/Extensions/HttpContextExtensionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void TryGetBasicAuthCredentials_WithHeader_ReturnsTrueWithCredentials()
2929

3030
var httpContext = new DefaultHttpContext();
3131
var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{testUsername}:{testPassword}"));
32-
httpContext.Request.Headers.Add("Authorization", $"Basic {credentials}");
32+
httpContext.Request.Headers.Append("Authorization", $"Basic {credentials}");
3333

3434
var result = httpContext.TryGetBasicAuthCredentials(out var username, out var password);
3535

tests/Umbraco.Tests.UnitTests/Umbraco.Web.Common/ModelBinders/HttpQueryStringModelBinderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private ModelBindingContext CreateBindingContext(string querystring)
7474
{
7575
var httpContext = new DefaultHttpContext();
7676
httpContext.Request.QueryString = new QueryString(querystring);
77-
httpContext.Request.Headers.Add("X-UMB-CULTURE", new StringValues("en-gb"));
77+
httpContext.Request.Headers.Append("X-UMB-CULTURE", new StringValues("en-gb"));
7878
var routeData = new RouteData();
7979
var actionContext = new ActionContext(httpContext, routeData, new ActionDescriptor());
8080
var metadataProvider = new EmptyModelMetadataProvider();

0 commit comments

Comments
 (0)