Skip to content

Commit 6e1b697

Browse files
committed
feat: add default favicon
1 parent 71eb3fd commit 6e1b697

File tree

5 files changed

+27
-8
lines changed

5 files changed

+27
-8
lines changed

DevWebServer.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.2</TargetFramework>
@@ -10,4 +10,8 @@
1010
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
1111
</ItemGroup>
1212

13+
<ItemGroup>
14+
<None Include="favicon.ico" CopyToPublishDirectory="PreserveNewest" />
15+
</ItemGroup>
16+
1317
</Project>

Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Collections;
3-
using System.Collections.Generic;
42
using System.Diagnostics;
53
using System.IO;
64
using Microsoft.AspNetCore;

SecureHeaderMiddleware.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ public Task InvokeAsync(HttpContext context)
3434
{
3535
headers[headerValuePair.Key] = headerValuePair.Value;
3636
}
37-
38-
// foreach (var header in result.RemoveHeaders)
39-
// {
40-
// headers.Remove(header);
41-
// }
4237
}
4338

4439
return Task.CompletedTask;

Startup.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Diagnostics;
33
using Microsoft.AspNetCore.Builder;
44
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.AspNetCore.Http;
56
using Microsoft.Extensions.Configuration;
67
using Microsoft.Extensions.DependencyInjection;
78
using Microsoft.Extensions.FileProviders;
@@ -38,6 +39,27 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env,
3839
app.UseMiddleware<SecureHeaderMiddleware>();
3940
}
4041

42+
// Provide default favicon if one is not specified in the html to prevent 404
43+
// This will probably prevent using a favicon with the default 'favicon.ico' name
44+
app.Use(async (context, next) =>
45+
{
46+
if (context.Request.Path.Equals("/favicon.ico", StringComparison.OrdinalIgnoreCase))
47+
{
48+
var fileInfo = env.ContentRootFileProvider.GetFileInfo("favicon.ico");
49+
50+
if (fileInfo.Exists)
51+
{
52+
HttpResponse response = context.Response;
53+
response.StatusCode = StatusCodes.Status200OK;
54+
response.ContentType = "image/vnd.microsoft.icon";
55+
await response.SendFileAsync(fileInfo);
56+
return;
57+
}
58+
}
59+
60+
await next();
61+
});
62+
4163
var config = configAccessor.Value;
4264

4365
Console.WriteLine($"Serving files from path {config.BaseDirectory}");

favicon.ico

1.12 KB
Binary file not shown.

0 commit comments

Comments
 (0)