基于域的 MVC 服务图标
MVC Serving Favicons based on Domain
现在我有一个将在多个域上发布的 Web 应用程序,我想支持基于域的不同 favicon
我所做的是:
** 在 web.config 中添加了一个名为“favicon”的处理程序,用于对名为“favicon.ico”
的文件的任何请求
<system.webServer>
<handlers>
<add name="favicon" verb="*" path="favicon.ico" type="namespace.FaviconHandler, MyApplication" />
// other handlers
</handlers>
</system.webServer>
** 然后添加 class 支持 IHttpHandler 接口
public class FaviconHandler : IHttpHandler
{
public void ProcessRequest(System.Web.HttpContext ctx)
{
string path = getFavIconPath(ctx.Request.Url.Host.ToLower());
string contentType = "image/x-icon";
path = ctx.Server.MapPath(path);
if (!File.Exists(path))
{
ctx.Response.StatusCode = 404;
ctx.Response.StatusDescription = "File not found";
}
else
{
ctx.Response.StatusCode = 200;
ctx.Response.ContentType = contentType;
ctx.Response.WriteFile(path);
}
}
private string getFavIconPath(string domain)
{
if (!string.IsNullOrEmpty(domain))
{
if (domain.Contains("abc.com")))
return "favicon.ico";
else
return "favicon2.ico";
}
return "favicon.ico";
}
}
问题是..它运行不正常..
我想念什么??
提前致谢
另一种方法是保留所有以域名命名的图标文件,例如 -
images
- abc.com.ico
- def.com.ico
创建一个 basecontroller 并在其 OnActionExecuting
中设置一个 ViewBag 属性(覆盖它)主机名 -
public override void OnActionExecuting(ActionExecutingContext ctx)
{
base.OnActionExecuting(ctx);
string host = HttpContext.Request.Host.Value;
ViewBag.Host = host;
}
并在您的主布局中设置网站图标 link,如 -
<link rel="icon" href="~/images/@(ViewBag.Host).ico"/>
现在我有一个将在多个域上发布的 Web 应用程序,我想支持基于域的不同 favicon
我所做的是:
** 在 web.config 中添加了一个名为“favicon”的处理程序,用于对名为“favicon.ico”
的文件的任何请求<system.webServer>
<handlers>
<add name="favicon" verb="*" path="favicon.ico" type="namespace.FaviconHandler, MyApplication" />
// other handlers
</handlers>
</system.webServer>
** 然后添加 class 支持 IHttpHandler 接口
public class FaviconHandler : IHttpHandler
{
public void ProcessRequest(System.Web.HttpContext ctx)
{
string path = getFavIconPath(ctx.Request.Url.Host.ToLower());
string contentType = "image/x-icon";
path = ctx.Server.MapPath(path);
if (!File.Exists(path))
{
ctx.Response.StatusCode = 404;
ctx.Response.StatusDescription = "File not found";
}
else
{
ctx.Response.StatusCode = 200;
ctx.Response.ContentType = contentType;
ctx.Response.WriteFile(path);
}
}
private string getFavIconPath(string domain)
{
if (!string.IsNullOrEmpty(domain))
{
if (domain.Contains("abc.com")))
return "favicon.ico";
else
return "favicon2.ico";
}
return "favicon.ico";
}
}
问题是..它运行不正常.. 我想念什么?? 提前致谢
另一种方法是保留所有以域名命名的图标文件,例如 -
images
- abc.com.ico
- def.com.ico
创建一个 basecontroller 并在其 OnActionExecuting
中设置一个 ViewBag 属性(覆盖它)主机名 -
public override void OnActionExecuting(ActionExecutingContext ctx)
{
base.OnActionExecuting(ctx);
string host = HttpContext.Request.Host.Value;
ViewBag.Host = host;
}
并在您的主布局中设置网站图标 link,如 -
<link rel="icon" href="~/images/@(ViewBag.Host).ico"/>