如何在 ASP.NET Core 的 Razor Pages 中设置全局变量? [英] How can I set a global variable in Razor Pages of ASP.NET Core?

查看:41
本文介绍了如何在 ASP.NET Core 的 Razor Pages 中设置全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查浏览器是否是 IE 并在 razor 页面中做一些事情.

I wanna to check if the browser is IE and do something in razor page.

我刚刚在 razor 页面中做了一个函数来做到这一点.

I just made a function in razor page to do that.

但是,我认为在每个剃须刀页面中使用该功能检查浏览器是否为IE是多余的.对于独立用户,我只需要检查一次并设置一个全局变量 IsIE=true/false .其他页面很容易知道是不是IE.

However, I think use the function to check if the browser is IE in every razor page is redundant. For independent user, I just need to check this only one time and set a global variable that IsIE=true/false . And other page will easily know that if it is IE.

问题是如何在 razor 页面中获取/设置全局变量?

The question is how can I get/set a global variable in razor page?

谢谢.

———————————————————

对@Neville Nazerane 来说,这是检查是否是 IE 的函数:

————————————————

To @Neville Nazerane ,here is the function which to check if is IE:

@{
    Boolean IsIE = false;
    string UA = Context.Request.Headers["User-Agent"].ToString();
    if (UA.Contains("Trident") || UA.Contains("MSIE"))
    {
        IsIE = true;
    }
    else
    {
        IsIE = false; ;
    }    
    if (IsIE == true)
    {
       
    }
    else
    {
       
    } 
}

推荐答案

HTTP 请求的工作原理是客户端向您的服务器发送请求(带有标头和正文).然后,您的服务器可以访问此信息并发送响应.这不会在服务器和客户端之间创建任何持久(正在进行的)连接.这意味着您的服务器和每个客户端之间没有永久链接.您声明的任何全局变量对于您的服务器的 Web 应用程序都是全局的,并且对于每个客户端都是通用的.

HTTP requests work by clients sending a request (with header and body) to your server. Your server can then access this info and send a response. This doesn't create any persistent (ongoing) connection between the server and client. This means there is no permanent link between your server and each client. Any global variable you declare will be global for your server's web application and will be common for every client.

您在这里尝试做的是创建与每个客户端的连接隔离的变量.通常这是在 SessionCookie 变量的帮助下完成的.但在这种情况下,我看不出这将如何提高您编写的代码的任何性能.在您的代码中,您试图从请求访问 Http 标头.Cookie 和会话变量也以非常相似的方式访问.如果直接从标题中获取任何内容,则必须具有稍微好一点的性能.如果您正在尝试清理代码以便不必在每个页面上都写这些,服务可能会非常有用.

What you are trying to do here is create variables isolated from each client's connection. Normally this is done with the help of Session or Cookie variable. But in this case, I don't see how this will improve any performance over the code you have written. In your code, you are trying to access the Http Headers from the request. Cookies and session variables are also accessed in a very similar way. If anything fetching directly from headers must have a slightly better performance. If you are trying to clean up your code so you don't have to write this on every page, services could be quite helpful.

您可以为服务创建一个类,如下所示:

You can create a class for service something like this:

public class AgentChecker
{

    public bool IsIE { get; set; }

    // makes sure check is done only when object is created
    public AgentChecker(IHttpContextAccessor accessor)
    {
        string UA = accessor.HttpContext.Request.Headers["User-Agent"].ToString();
        if (UA.Contains("Trident") || UA.Contains("MSIE"))
        {
            IsIE = true;
        }
        else
        {
            IsIE = false; 
        }
    }

    // optional to simplify usage further. 
    public static implicit operator bool(AgentChecker checker) => checker.IsIE;

}

在您的启动类中添加以下内容:

In your startup class add the following:

// to access http context in a service
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
// makes sure object is created once per request
services.AddScoped<AgentChecker>();

设置完成后,您可以在视图中使用:

Once this is set up, in your view you can use:

@inject AgentChecker checker

@* if you didn't create the implicit operator, you can use if (checker.IsIE) *@
@if (checker)
{
    <div>Is ie</div>
}
else
{
    <div>not ie</div>
}

inject 位于您想在其中使用它的任何视图页面的顶部.虽然这仍然会在每个请求中创建一个新对象,但使用起来更干净,并且无论如何只创建一个对象您使用了多少部分视图.

The inject goes at the top of any view page you would like to use this in. While this still creates a new object each request, it is cleaner to use and only creates one object no matter how many partial views you are using.

这篇关于如何在 ASP.NET Core 的 Razor Pages 中设置全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆