Home > Blockchain >  How to port cookie.Value from .NET 5 to .NET 6/7? (ASP.NET Core)
How to port cookie.Value from .NET 5 to .NET 6/7? (ASP.NET Core)

Time:11-22

The code in question is:

var arrVal = cookie.Value.Split('.');

I tried the following syntax according to the documentation, but it doesn't seem to work.

var arrVal = cookie["Value"].Split('.');

For context rest of the code:

public IList<CookieType> GetAcceptedCookieTypes(HttpContext httpContext)
{
    var result = new List<CookieType>();

    // accepted by default
    result.Add(CookieType.Essential);

    var cookie = httpContext.Request.Cookies["cc_cookie_accept"];

    if (cookie != null)
    {
        var arrVal = cookie.Value.Split('.');

        if (arrVal != null && arrVal.Length > 1)
        {
            var arrCheck = arrVal[1];

            if (arrCheck.Length > 0 && arrCheck[0] == '1')
            {
                result.Add(CookieType.Statistical);
            }
        }
    }

    return result;
}

This is the error I'm getting:

CS1061: 'string' does not contain a definition for 'Value' and no accessible extension method 'Value' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

CodePudding user response:

I checked the docs and IRequestCookieCollection[String] has always returned string? going back to ASP.NET Core 1.0. Your code could not have compiled against ASP.NET Core 5.0 where the only change was to add the nullable annotation.

I note that in ASP.NET for .NET Framework (2001-2015) the Request.Cookies collection did return System.Web.HttpCookie objects which do have a .Value property, but that's not .NET 5 at all.

Anyway. to fix it just remove the .Value part.

  • Related