簡體   English   中英

將Favicon動態加載到共享的_Layout.cshtml中

[英]Load favicon dynamically into shared _Layout.cshtml

如何動態地(根據條件根據編程)將favicon添加到共享的_Layout.cshtml的Razor中? 主布局。

沒有更多細節,基本上是:

<head>
@if (true)
{
    <link rel="shortcut icon" href="http://example.com/favicon.ico" />
}
else
{
    <link rel="shortcut icon" href="http://example.com/favicon2.ico" />
}
</head>

您可以在_Layout中執行子操作

@Html.Action("Favicon", "MyController", new { parameter = "value" })

采取行動

[ChildActionOnly]
public ActionResult Favicon(string parameter)
{
    string url = GetFaviconUrl(parameter);
    ViewBag.FaviconUrl = url;
    return PartialView();
}

和偏

<link rel="shortcut icon" href="@ViewBag.FaviconUrl" />

我知道這個問題很舊,但我只是必須這樣做,卻沒有在網上找到適合我需要的好方法,所以我發布此答案以給其他人可能有用的方法。

我有一個健康狀態頁面,希望能夠看到紅色,黃色或綠色圖標作為圖標,以便在我研究另一個問題時,我可以快速檢查公司應用程序的狀態。 狀態頁面上的每個部分都是一個ViewComponent,所有這些都從Index.cshtml調用

通過嘗試各種方法,我發現在渲染所有視圖組件之后,將_Layout.cshtml稱為,這大大簡化了此過程。 所有View組件均設置其特定狀態,例如ApiStatusViewComponent設置Shared.ApiStatus變量。 每次調用時,overallStatus都會更新(我知道這是浪費的處理器周期,但它可以工作)。 到Index.cshtml調用_Layout.cshtml時,overallStatus已經反映了正確的值。

下面的代碼示例:

_Layout.cshtml

<link rel="icon" type="image/png" sizes="923x923" href=@Shared.GetFavIcon() />

我知道這是一個大圖標,但瀏覽器將其縮小到平台所需的大小。

Shared.cs

public static class Shared
    {
        private static OverallStatusEnum overallStatus;

        public static string GetFavIcon()
        {
            switch (overallStatus)
            {
                case OverallStatusEnum.Ok:
                    return "https://PathToServer/Green.png";
                case OverallStatusEnum.Warning:
                    return "https://PathToServer/Yellow.png";
                case OverallStatusEnum.Error:
                    return "https://PathToServer/Red.png";
                default:
                    return "https://PathToServer/Yellow.png";
            }
        }

        public static class Status
        {
            private static OverallStatusEnum apiStatus;
            private static OverallStatusEnum criticalServicesStatus;

            // When adding a new section to monitor, add the overall status here so the favicon can be updated
            public static OverallStatusEnum ApiStatus { get => apiStatus; set => UpdateStatus(value, nameof(ApiStatus)); }
            public static OverallStatusEnum CriticalServicesStatus { get => criticalServicesStatus; set => UpdateStatus(value, nameof(CriticalServicesStatus)); }

            private static void UpdateStatus(OverallStatusEnum status, string propertyName)
            {
                switch(propertyName)
                {
                    case nameof(ApiStatus):
                        apiStatus = status;
                        break;
                    case nameof(CriticalServicesStatus):
                        criticalServicesStatus = status;
                        break;
                }

                overallStatus = (OverallStatusEnum)GetHighestStatusLevel();
            }

            private static int GetHighestStatusLevel()
            {
                var highestLevel = -1;
                var type = typeof(Status);
                foreach (var p in type.GetFields(BindingFlags.Static | BindingFlags.NonPublic))
                {
                    var v = p.GetValue(null); // static classes cannot be instanced, so use null...
                    if (highestLevel > (int)v || highestLevel == (int)v)
                        continue;
                    highestLevel = (int)v;
                }
                return highestLevel;
            }
        }
    }

總體狀態枚舉

public enum OverallStatusEnum
        {
            Ok,
            Warning,
            Error
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM