I set a cookie using JavaScript when the user clicks a button:
document.cookie = "menuSize=Large";
I need to access this cookie in razor syntax so I can output the correct styles at the top of _Layout.cshtml every time the user changes pages:
@{
if (cookie == "Large")
{
<style>
LARGE STYLES
</style>
}
else
{
<style>
SMALL STYLES
</style>
}
}
In ASP.Net MVC application, a Cookie is created by sending the Cookie to Browser through Response collection (Response. Cookies) while the Cookie is accessed (read) from the Browser using the Request collection (Request. Cookies).
A Razor Page is almost the same as ASP.NET MVC's view component. It has basically the syntax and functionality same as MVC. The basic difference between Razor pages and MVC is that the model and controller code is also added within the Razor Page itself. You do not need to add code separately.
Razor markup is code that interacts with HTML markup to produce a webpage that's sent to the client. In ASP.NET Core MVC, views are .cshtml files that use the C# programming language in Razor markup.
I was having the same problem and did it:
@using Microsoft.AspNetCore.Http;
@{
if (HttpContext.Request.Cookies["menuSize"].Value == "Large")
{
<style>
LARGE STYLES
</style>
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With