Tomato
Tomato

Reputation: 122

Blazor server side - Is there any way to get a Cookie value during the initialization?

Would like to get a cookie value during app initialization in Startup.cs, but after an extensive search i cannot find any solution. I can get the cookies from Razor pages and aware about some of the storage NuGet packages.

To clarify a little bit more:
I would like a similar cookie, what language&culture modules have: the RequestCultureProvider can read that cookie(s) during Configuration, but it has special methods, functions, etc.. Official MicroSoft description:
https://learn.microsoft.com/en-us/aspnet/core/blazor/globalization-localization?view=aspnetcore-5.0

In the previous example, if the user would like to change the language, call a controller to reload the new page with the associated language info and saved the value in a cookie.
If i cannot get the last used language value from a cookie during Startup, the initial page needs to reload one-more times, when i change to a new language....

So i would like something similar, e.g.: there is a currency selector on every page and would like to store their value in a cookie, so next time the user visit the page, it shows the right currency - without the need of page reloading(s).

Any solution are welcome.

Upvotes: 1

Views: 5175

Answers (1)

enet
enet

Reputation: 45674

You can simply define a service that, when you need to save a given currency, you call its set method to save this value in the local storage. You don't need cookie for this. Of course you can use cookie for this if you want, but it is simpler to do that with the local storage.

Now, whenever you want to retrieve this value you call the get method of the service. You can inject this service into each component of your app. Actually, you shouldn't inject it but into a single component, you may call CurrencySelector, and embedded in the MainLayout component, just in place of the CultureSelector in my previous answer's code. No reloading, no refreshing, only re-rendering of the CurrencySelector component

Your service may look like this:

public class CurrencyProvider
{
    private readonly IJSRuntime _jsRuntime;

    public CurrencyProvider (IJSRuntime jsRuntime)
    {
        _jsRuntime = jsRuntime;
    }

    public async Task<string> GetCurrencyAsync()
        => await _jsRuntime.InvokeAsync<string>("localStorage.getItem", "currencyType");

    public async Task SetCurrencyAsync(string currency)
    {
        if (currency == null)
        {
            await _jsRuntime.InvokeAsync<object>("localStorage.removeItem", "currencyType");
        }
        else
        {
            await _jsRuntime.InvokeAsync<object>("localStorage.setItem", "currencyType", currency);
        }

      
    }
}

UPDATE:

Thank you for your answer and time. This can be a solution, however my original question was about "Would like to get a cookie value during app initialization in Startup.cs". Is it possible?

Yes, it's possible. You should create a middleware, and access the HttpContext to read the cookie.

Note: The following code describes how you can access a cookie when your app is being initialized. This is the easiest and right solution:

Place the following code snippet at the top of the _Host.cshtml file:

@{
    var myCookie = HttpContext.Request.Cookies.FirstOrDefault(c => c.Key == ".AspNetCore.Cookies");

}

Do whatever you want to do with the cookie. If you want to pass values to the Blazor SPA, define a parameter property in the App component, and pass it a value from the _Host.cshtml file via the component html tag helper.

Note: The implementation of the CurrencyProvider above is a more suitable way of storing client data for Blazor apps because cookies are sent with every request, while the local storage holds data that is freely available to your Blazor app.

Upvotes: 2

Related Questions