If you are meaning to show a favicon for the CMS backoffice itself, then I have an old package called CMS Environment Indicator for Umbraco v7, that injects a JavaScript file into the backoffice on initial page load, that will set the favicon colour based on the current domain/hostname for different environments (dev, staging, production).
You could adapt this JavaScript to fit your own needs.
Alternatively, if you are meaning for the frontend, e.g. non-backoffice requests, then there could be a way to hook into ASP.NET Core middleware to intercept any requests to the /favicon.ico URL, and serve up the website's corresponding favicon. (I don't currently have a code example for this, sorry).
Following up on this, it turns out that you may not need the Middleware to route the request to a controller, you could use the [Route] attribute. No need for registration in the Startup class.
Something like this...
using Microsoft.AspNetCore.Mvc;
namespace Our.Umbraco.Web.Controllers;
public class FaviconIcoController : Controller
{
private readonly IWebHostEnvironment _webHostEnvironment;
public FaviconIcoController(IWebHostEnvironment webHostEnvironment)
{
_webHostEnvironment = webHostEnvironment;
}
[Route("/favicon.ico")]
public IActionResult Index()
{
var hostname = Request.Host.Host;
var path = Path.Combine(_webHostEnvironment.WebRootPath, $"{hostname}.ico");
if (System.IO.File.Exists(path) == true)
{
var bytes = System.IO.File.ReadAllBytes(path);
return File(bytes, "image/x-icon");
}
return NotFound("favicon.ico not found");
}
}
Please note that the [Route] attribute only works if the physical file for "favicon.ico" does NOT exist, otherwise the static file provider will kick in and serve that before it hits the controller.
I'd assume it'd work - as it's pretty much routing a request, but the same logic applies that if a physical file with the same name exists, then then static file provider would pick it up first.
My only caveat would be that I'm not familiar with the *.stp file extension, so don't know if a MIME type would need to be configured beforehand.
Prepare individual favicon.ico files for each website. Check they are appropriately named like faviconsite1.ico, faviconsite2.ico, etc.
after that upload each favicon file to the respective media section in Umbraco and then edit the Backoffice view for each website to include a link to the corresponding favicon.
Onpen the view file.
How to set favicon for Backoffice based on domain
Hi,
I have multiple websites within a single Umbraco installation. How can I set distinct favicons for each website Backoffice?
I need to load different favicons for media files (pdf/images) opened in a new tab for each domain.
Thanks
Hi Ginu,
If you are meaning to show a favicon for the CMS backoffice itself, then I have an old package called CMS Environment Indicator for Umbraco v7, that injects a JavaScript file into the backoffice on initial page load, that will set the favicon colour based on the current domain/hostname for different environments (dev, staging, production).
The JavaScript is here... https://github.com/leekelleher/umbraco-environment-indicator/blob/develop/src/App_Plugins/CmsEnvironmentIndicator/js/cms-environment-indicator.js
You could adapt this JavaScript to fit your own needs.
Alternatively, if you are meaning for the frontend, e.g. non-backoffice requests, then there could be a way to hook into ASP.NET Core middleware to intercept any requests to the
/favicon.ico
URL, and serve up the website's corresponding favicon. (I don't currently have a code example for this, sorry).Cheers,
- Lee
Following up on this, it turns out that you may not need the Middleware to route the request to a controller, you could use the
[Route]
attribute. No need for registration in the Startup class.Something like this...
Please note that the
[Route]
attribute only works if the physical file for "favicon.ico" does NOT exist, otherwise the static file provider will kick in and serve that before it hits the controller.Hack the code as you'd need.
Hope this helps.
Cheers,
- Lee
Interesting response Lee. Can you wildcard the Route? So say if you wanted to catch all requests for a certain file type e.g. stp?
Hi Brendan,
I'd assume it'd work - as it's pretty much routing a request, but the same logic applies that if a physical file with the same name exists, then then static file provider would pick it up first.
My only caveat would be that I'm not familiar with the
*.stp
file extension, so don't know if a MIME type would need to be configured beforehand.Cheers,
- Lee
Prepare individual favicon.ico files for each website. Check they are appropriately named like faviconsite1.ico, faviconsite2.ico, etc. after that upload each favicon file to the respective media section in Umbraco and then edit the Backoffice view for each website to include a link to the corresponding favicon. Onpen the view file.
Update the link tag for the favicon based on the domain.
Save the changes and test by accessing the Backoffice for each website.
is working on a reply...