【问题标题】:Detect browser and not load HTML, cshtml cSharp检测浏览器,不加载 HTML,cshtml cSharp
【发布时间】:2019-09-09 16:17:07
【问题描述】:

是否有一个简单的 csharp (cshtml) 解决方案来检测浏览器是否是 Chrome,然后 不是 加载一段 html 代码。或者,我知道我可能只使用 javascript,但使用 C# html 组合会很好。

    @Html.Partial("_Navbar")
    <div class="container-fluid">
        @RenderBody()

<!-- only load footer if browser is Chrome -->
        <footer>
            <div class="row">
                <div class="col l-foot">
                    &copy;@DateTime.Now.Year 
                </div>
                <div class="col r-foot">
                    <p>@Html.Raw($"Test - {layoutModel.Version}")</p>
                </div>
            </div>
        </footer>
    </div>

【问题讨论】:

  • 请注意,服务器端浏览器检测取决于浏览器自身的信息以及服务器端对其的了解。简而言之,它可能会失败

标签: javascript c# .net


【解决方案1】:

当然,

@if (Request.Browser.Browser == "Chrome")
{
    @Html.Raw("<<<<HTML HERE>>>>")
}
else
{
    @Html.Raw("<<<<ALTERNATE HTML HERE>>>>")
}

【讨论】:

    【解决方案2】:

    您也可以在控制器中执行此操作,如果浏览器是 chrome,则返回不同的视图:

    public IActionResult Get()
    {
        if (this.Request.Browser == "Chrome")
        {
            return View("ViewForChrome");
        }
        else
        {
            return View("OtherView");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-20
      • 1970-01-01
      • 2016-01-06
      • 1970-01-01
      • 2010-10-19
      • 1970-01-01
      • 2011-01-14
      相关资源
      最近更新 更多