Swazimodo
Swazimodo

Reputation: 1239

MVC favicon.ico routing for WebAPI

I have a ASP.NET Core MVC/WebAPI project using .NET Framework 4.6.2. In my solution I have different favicon images for each environment (DEV, Staging, PROD). When a MVC page is displayed I am able to point the browser to the correct favicon for that environment without issue. However, any API GET calls done directly from a web browser, it always default to favicon.ico at the site root. Is there anyway to make it route to a configurable value so that I can choose which one will be shown?

Upvotes: 2

Views: 1600

Answers (1)

RonC
RonC

Reputation: 33791

You can do that via middleware. Just make sure the UseMiddleware<YourCustomMiddlware> in the Startup.cs Configure method comes before UseStaticFiles(). That way it will get an oppertunity to intercept the request and respond to it before the regular static file middleware does.

In your custom middleware you just need to check the url of the request and if it's for the fav icon at the root, then you can check whether you are running in Dev, Staging or PROD and return the actual file you want for the file you are in.

Upvotes: 2

Related Questions