简体   繁体   English

将Favicon动态加载到共享的_Layout.cshtml中

[英]Load favicon dynamically into shared _Layout.cshtml

How to add favicon dynamically(programatically based on conditions) into the Razor of shared _Layout.cshtml ? 如何动态地(根据条件根据编程)将favicon添加到共享的_Layout.cshtml的Razor中? The master layout. 主布局。

Without more details it's basically: 没有更多细节,基本上是:

<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>

You could do a child action in the _Layout 您可以在_Layout中执行子操作

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

With an action 采取行动

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

And the partial 和偏

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

I know this question is old, but I just had to do this and didn't find a good way online that would fit what I needed so I am posting this answer to give others another way that might help. 我知道这个问题很旧,但我只是必须这样做,却没有在网上找到适合我需要的好方法,所以我发布此答案以给其他人可能有用的方法。

I have a health status page and wanted to be able to see a red, yellow or green icon as the favicon so as I'm on SO researching another issue, I can quickly check the status of the company apps. 我有一个健康状态页面,希望能够看到红色,黄色或绿色图标作为图标,以便在我研究另一个问题时,我可以快速检查公司应用程序的状态。 Each section on the status page is a ViewComponent and all of them are called from Index.cshtml 状态页面上的每个部分都是一个ViewComponent,所有这些都从Index.cshtml调用

Through trying various things, I found out that the _Layout.cshtml gets called AFTER all the View Components get rendered which greatly simplified this process. 通过尝试各种方法,我发现在渲染所有视图组件之后,将_Layout.cshtml称为,这大大简化了此过程。 All the View Components set their specific status, for example the ApiStatusViewComponent sets the Shared.ApiStatus variable. 所有View组件均设置其特定状态,例如ApiStatusViewComponent设置Shared.ApiStatus变量。 On each call, the overallStatus gets updated (wasted processor cycles I know but it works). 每次调用时,overallStatus都会更新(我知道这是浪费的处理器周期,但它可以工作)。 By the time Index.cshtml calls _Layout.cshtml, the overallStatus is already reflecting the correct value. 到Index.cshtml调用_Layout.cshtml时,overallStatus已经反映了正确的值。

Code examples below: 下面的代码示例:

_Layout.cshtml _Layout.cshtml

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

It's a big icon I know, but the browsers get it shrunk down to the size needed for the platform. 我知道这是一个大图标,但浏览器将其缩小到平台所需的大小。

Shared.cs 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;
            }
        }
    }

OverallStatusEnum 总体状态枚举

public enum OverallStatusEnum
        {
            Ok,
            Warning,
            Error
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM