How can I set a global variable in Razor Pages of ASP.NET Core?
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.
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>
}
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.
Melon NG
Updated on June 18, 2022Comments
-
Melon NG almost 2 years
I wanna to check if the browser is IE and do something in razor page.
I just made a function in razor page to do that.
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.The question is how can I get/set a global variable in razor page?
Thank you.
————————————————
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 { } }
-
Melon NG about 6 yearsI think it is the correct answer,but I wonder why do not use cookies?
-
Neville Nazerane about 6 yearsYou can use cookies. But in this case, you will not get any advantage. Cookies are also passed with your HTTP header and your server needs to go through the same process. If anything that will be more work for your server and for you
-
FranzHuber23 about 4 yearsBy the way, you don't need the
HttpContextAccessor
. You can directly referencethis.Request.HttpContext.Request.Headers["User-Agent"]
once you add theMicrosoft.AspNetCore.HttpOverrides
nuget package.