• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MTY0NA==

Related Code
Fork of GZIP your Actions in MVC if not used as standard in IIS7 or only use IIS6
ActionFilter to Handle and Log Error for each Controller, ASP.NET MVC
GZIP your Actions in MVC if not used as standard in IIS7 or only use IIS6
Using DynamicObject to get a value from the Request.Params
PagedList class and paging code for ASP.Net MVC (Nothing new but kind of usefull.)
Removing properties from the model state the typed way in ASP.NET MVC
A simple way to cache slow functions in C#
Fork of Serialize an object into a Querystring, using LINQ syntax
ASP.NET MVC 4.0 Web API Message logger for .Net 4.5
Serialize an object into a Querystring
HttpContextLifetimeManager for Unity
ASP.NET MVC 4.0 Web API Message logger for .Net 4.0
Extension to the HttpClient

Get Cookies in Web Api Asp .net Mvc 4 RC

Code how to retrieve cookies in Web Api Calls

0
1.11k 0 0 0 0 0

Here is an extension method for HttpRequestHeader in Web Api to get Cookies.

public static class HttpRequestHeaderExtension
{
    public static string GetCookie(this HttpRequestHeaders header, string name)
    {
        try
        {
            var cookies = GetCookies(header);
            return cookies.Select(cookie => cookie[name].Value).FirstOrDefault(value => value != null);
        }
        catch (Exception)
        {
            return null;
        }
    }

    private static IEnumerable<CookieHeaderValue> GetCookies(HttpRequestHeaders header)
    {
        var result = new Collection<CookieHeaderValue>();
        IEnumerable<string> cookies;
        if (header.TryGetValues("Cookie", out cookies))
        {
            foreach (string cookie in cookies)
            {
                CookieHeaderValue cookieHeaderValue;
                if (CookieHeaderValue.TryParse(cookie, out cookieHeaderValue))
                {
                    result.Add(cookieHeaderValue);
                }
            }
        }
        return result;
    }
}

}

And to call it on Action filter etc.

Request.Headers.GetCookie("cookieName");


Share: twitter | facebook   Action: used | fork | flag

asp.net

Mark 'asp.net' tag as 'like'

Mark 'asp.net' tag as 'ignore'

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'

mvc

Mark 'mvc' tag as 'like'

Mark 'mvc' tag as 'ignore'

webapi

Mark 'webapi' tag as 'like'

Mark 'webapi' tag as 'ignore'


 @johannormen "Code Contributor"
1.24k
July 04, 2012 1:37 PM

Fork

 Get Cookies in Web Api Asp .net Mvc 4 RC -  @johannormen Wednesday 04, 2012 1:37 PM


0 Feedback


You must log in before you can give any feedback


0 Discussion(s)

Newest Oldest

You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010