Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to port cookie.Value from .NET 5 to .NET 6/7? (ASP.NET CORE)

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
        }

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?)

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading