Has Microsoft created a class full of constants for the standard HTTP header names or will I have to write my own?
using System.Net.HttpRequestHeader;
using System.Net.HttpResponseHeader;
public class Example {
static void Main() {
Console.WriteLine(HttpRequestHeader.IfModifiedSince.ToHeaderString());
// If-Modified-Since
Console.WriteLine(HttpResponseHeader.ContentLength.ToHeaderString());
// Content-Length
}
}
static class ExtensionMethods {
public static string ToHeaderString(this HttpRequestHeader instance)
{
return Regex.Replace(instance.ToString(), "(\\B[A-Z])", "-$1");
}
public static string ToHeaderString(this HttpResponseHeader instance)
{
return Regex.Replace(instance.ToString(), "(\\B[A-Z])", "-$1");
}
}
I found this question while trying to discover the same thing: where are the header name constants as strings?
In ASP.NET Core, Microsoft.Net.Http.Headers.HeaderNames
is the class that saved me.
public static class HeaderNames
{
public const string Accept = "Accept";
public const string AcceptCharset = "Accept-Charset";
public const string AcceptEncoding = "Accept-Encoding";
public const string AcceptLanguage = "Accept-Language";
public const string AcceptRanges = "Accept-Ranges";
public const string Age = "Age";
public const string Allow = "Allow";
public const string Authorization = "Authorization";
public const string CacheControl = "Cache-Control";
public const string Connection = "Connection";
public const string ContentDisposition = "Content-Disposition";
public const string ContentEncoding = "Content-Encoding";
public const string ContentLanguage = "Content-Language";
public const string ContentLength = "Content-Length";
public const string ContentLocation = "Content-Location";
public const string ContentMD5 = "ContentMD5";
public const string ContentRange = "Content-Range";
public const string ContentType = "Content-Type";
public const string Cookie = "Cookie";
public const string Date = "Date";
public const string ETag = "ETag";
public const string Expires = "Expires";
public const string Expect = "Expect";
public const string From = "From";
public const string Host = "Host";
public const string IfMatch = "If-Match";
public const string IfModifiedSince = "If-Modified-Since";
public const string IfNoneMatch = "If-None-Match";
public const string IfRange = "If-Range";
public const string IfUnmodifiedSince = "If-Unmodified-Since";
public const string LastModified = "Last-Modified";
public const string Location = "Location";
public const string MaxForwards = "Max-Forwards";
public const string Pragma = "Pragma";
public const string ProxyAuthenticate = "Proxy-Authenticate";
public const string ProxyAuthorization = "Proxy-Authorization";
public const string Range = "Range";
public const string Referer = "Referer";
public const string RetryAfter = "Retry-After";
public const string Server = "Server";
public const string SetCookie = "Set-Cookie";
public const string TE = "TE";
public const string Trailer = "Trailer";
public const string TransferEncoding = "Transfer-Encoding";
public const string Upgrade = "Upgrade";
public const string UserAgent = "User-Agent";
public const string Vary = "Vary";
public const string Via = "Via";
public const string Warning = "Warning";
public const string WebSocketSubProtocols = "Sec-WebSocket-Protocol";
public const string WWWAuthenticate = "WWW-Authenticate";
}
If you're using .NET Framework (not .NET Core), you can create an extension method to properly format the System.Net.HttpRequestHeader
enum:
using System;
using System.Net;
using System.Text;
namespace YourNamespace
{
public static class HttpRequestHeaderEx
{
public static string ToStandardName(this HttpRequestHeader requestHeader)
{
string headerName = requestHeader.ToString();
var headerStandardNameBuilder = new StringBuilder();
headerStandardNameBuilder.Append(headerName[0]);
for (int index = 1; index < headerName.Length; index++)
{
char character = headerName[index];
if (char.IsUpper(character))
{
headerStandardNameBuilder.Append('-');
}
headerStandardNameBuilder.Append(character);
}
string headerStandardName = headerStandardNameBuilder.ToString();
return headerStandardName;
}
}
}
Usage:
var userAgentHeaderName = HttpRequestHeader.UserAgent.ToStandardName();
There are some available in the Microsoft.Net.Http.Headers nuget package. In my asp.net core project it was already installed.
Example usage:
var value = request.Headers[Microsoft.Net.Http.Headers.HeaderNames.IfNoneMatch]
Might be what some are looking for?
They have them in HttpKnownHeaderNames but unfortunately that class is internal. I opened an issue for it: https://github.com/dotnet/corefx/issues/10632.
To expand on Jed's answer.
The HttpResponseHeader and HttpRequestHeader enumerations can be used as your constants when using the WebHeaderCollection. WebHeaderCollection
contains indexer properties that accept these enumerations.
You can use either a string or one of the enumerations to get and set the header value, and mix it up as well within you code.
Example LinqPad script:
var headers = new WebHeaderCollection();
headers["If-Modified-Since"] = "Sat, 29 Oct 1994 19:43:31 GMT";
// shows header name as "If-Modified-Since"
headers.Dump();
// shows expected header value of "Sat, 29 Oct 1994 19:43:31 GMT"
headers[HttpRequestHeader.IfModifiedSince].Dump();
headers.Clear();
headers[HttpRequestHeader.IfModifiedSince] = "Sat, 29 Oct 1994 19:43:31 GMT";
// shows header name as "If-Modified-Since"
headers.Dump();
// shows expected header value "Sat, 29 Oct 1994 19:43:31 GMT"
headers["If-Modified-Since"].Dump();