2016-02-23 41 views
2

MVC服務網站圖標現在我將在多個領域進行發佈的Web應用程序,我想基於域基於域

我做了什麼,以支持不同的圖標是:

**增加了處理程序在web.config中稱爲「圖標」,對於名爲「的favicon.ico」

<system.webServer> 
<handlers> 
<add name="favicon" verb="*" path="favicon.ico" type="namespace.FaviconHandler, MyApplication" /> 
// other handlers 
</handlers> 
</system.webServer> 

對文件的任何請求**然後加入類支持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"; 
     } 
    } 

問題是..它運作不好.. 我想念什麼? 在此先感謝

+0

難道你不能用JavaScript實現這個更簡單嗎?只需爲特定於域的文件夾中的每個域添加一個收藏夾圖標,然後根據window.location在客戶端上構建收藏夾圖標的URL。然後在頭標籤中設置它? – LDJ

回答

2

另一種方式可以跟上,比如域名命名的所有圖標文件 -

images 
    - abc.com.ico 
    - def.com.ico 

做一個basecontroller和設置ViewBag屬性的OnActionExecuting(覆蓋它)與主機名 -

public override void OnActionExecuting(ActionExecutingContext ctx) 
{ 
    base.OnActionExecuting(ctx); 
    string host = HttpContext.Request.Host.Value; 
    ViewBag.Host = host; 
} 

而且像你的主人佈局設置圖標鏈接 -

<link rel="icon" href="~/images/@(ViewBag.Host).ico"/>