2012-06-14 4 views
16

असल में, एक ही प्रश्न मैंने डिफ़ॉल्ट सामग्री प्रकारों के लिए पूछा लेकिन इस बार हेडर के लिए: क्या माइक्रोसॉफ्ट ने मानक HTTP शीर्षलेख नामों के लिए स्थिरांक से भरा वर्ग बनाया है या क्या मुझे अपना खुद लिखना होगा?एएसपी एमवीसी - क्या डिफ़ॉल्ट HTTP हेडर के लिए कोई स्थिरांक है?

धन्यवाद, ल्यूक

उत्तर

15

अनुरोध हेडर

/// <summary> 
/// Contains the standard set of headers applicable to an HTTP request. 
/// </summary> 
public static class HttpRequestHeaders 
{ 
    ///<summary>Content-Types that are acceptable</summary> 
    public const string Accept = "Accept"; 
    ///<summary>Character sets that are acceptable</summary> 
    public const string AcceptCharset = "Accept-Charset"; 
    ///<summary>Acceptable encodings. See HTTP compression.</summary> 
    public const string AcceptEncoding = "Accept-Encoding"; 
    ///<summary>Acceptable languages for response</summary> 
    public const string AcceptLanguage = "Accept-Language"; 
    ///<summary>Acceptable version in time</summary> 
    public const string AcceptDatetime = "Accept-Datetime"; 
    ///<summary>Authentication credentials for HTTP authentication</summary> 
    public const string Authorization = "Authorization"; 
    ///<summary>Used to specify directives that MUST be obeyed by all caching mechanisms along the request/response chain</summary> 
    public const string CacheControl = "Cache-Control"; 
    ///<summary>What type of connection the user-agent would prefer</summary> 
    public const string Connection = "Connection"; 
    ///<summary>an HTTP cookie previously sent by the server with Set-Cookie (below)</summary> 
    public const string Cookie = "Cookie"; 
    ///<summary>The length of the request body in octets (8-bit bytes)</summary> 
    public const string ContentLength = "Content-Length"; 
    ///<summary>A Base64-encoded binary MD5 sum of the content of the request body</summary> 
    public const string ContentMD5 = "Content-MD5"; 
    ///<summary>The MIME type of the body of the request (used with POST and PUT requests)</summary> 
    public const string ContentType = "Content-Type"; 
    ///<summary>The date and time that the message was sent</summary> 
    public const string Date = "Date"; 
    ///<summary>Indicates that particular server behaviors are required by the client</summary> 
    public const string Expect = "Expect"; 
    ///<summary>The email address of the user making the request</summary> 
    public const string From = "From"; 
    ///<summary>The domain name of the server (for virtual hosting), mandatory since HTTP/1.1. Although domain name are specified as case-insensitive[5][6], it is not specified whether the contents of the Host field should be interpreted in a case-insensitive manner[7] and in practice some implementations of virtual hosting interpret the contents of the Host field in a case-sensitive manner.[citation needed]</summary> 
    public const string Host = "Host"; 
    ///<summary>Only perform the action if the client supplied entity matches the same entity on the server. This is mainly for methods like PUT to only update a resource if it has not been modified since the user last updated it.</summary> 
    public const string IfMatch = "If-Match"; 
    ///<summary>Allows a 304 Not Modified to be returned if content is unchanged</summary> 
    public const string IfModifiedSince = "If-Modified-Since"; 
    ///<summary>Allows a 304 Not Modified to be returned if content is unchanged, see HTTP ETag</summary> 
    public const string IfNoneMatch = "If-None-Match"; 
    ///<summary>If the entity is unchanged, send me the part(s) that I am missing; otherwise, send me the entire new entity</summary> 
    public const string IfRange = "If-Range"; 
    ///<summary>Only send the response if the entity has not been modified since a specific time.</summary> 
    public const string IfUnmodifiedSince = "If-Unmodified-Since"; 
    ///<summary>Limit the number of times the message can be forwarded through proxies or gateways.</summary> 
    public const string MaxForwards = "Max-Forwards"; 
    ///<summary>Implementation-specific headers that may have various effects anywhere along the request-response chain.</summary> 
    public const string Pragma = "Pragma"; 
    ///<summary>Authorization credentials for connecting to a proxy.</summary> 
    public const string ProxyAuthorization = "Proxy-Authorization"; 
    ///<summary>Request only part of an entity. Bytes are numbered from 0.</summary> 
    public const string Range = "Range"; 
    ///<summary>This is the address of the previous web page from which a link to the currently requested page was followed. (The word “referrer” is misspelled in the RFC as well as in most implementations.)</summary> 
    public const string Referersic = "Referer[sic]"; 
    ///<summary>The transfer encodings the user agent is willing to accept: the same values as for the response header Transfer-Encoding can be used, plus the trailers value (related to the chunked transfer method) to notify the server it expects to receive additional headers (the trailers) after the last, zero-sized, chunk.</summary> 
    public const string TE = "TE"; 
    ///<summary>Ask the server to upgrade to another protocol.</summary> 
    public const string Upgrade = "Upgrade"; 
    ///<summary>The user agent string of the user agent</summary> 
    public const string UserAgent = "User-Agent"; 
    ///<summary>Informs the server of proxies through which the request was sent.</summary> 
    public const string Via = "Via"; 
    ///<summary>A general warning about possible problems with the entity body.</summary> 
    public const string Warning = "Warning"; 
    ///<summary>Contains the original source address of the request.</summary> 
    public const string XForwardedFor = "X-Forwarded-For"; 
} 

प्रतिक्रिया हेडर

/// <summary> 
/// Contains the standard set of headers applicable to an HTTP response. 
/// </summary> 
public static class HttpResponseHeaders 
{ 
    ///<summary>What partial content range types this server supports</summary> 
    public const string AcceptRanges = "Accept-Ranges"; 
    ///<summary>The age the object has been in a proxy cache in seconds</summary> 
    public const string Age = "Age"; 
    ///<summary>Valid actions for a specified resource. To be used for a 405 Method not allowed</summary> 
    public const string Allow = "Allow"; 
    ///<summary>Tells all caching mechanisms from server to client whether they may cache this object. It is measured in seconds</summary> 
    public const string CacheControl = "Cache-Control"; 
    ///<summary>Options that are desired for the connection[17]</summary> 
    public const string Connection = "Connection"; 
    ///<summary>The type of encoding used on the data. See HTTP compression.</summary> 
    public const string ContentEncoding = "Content-Encoding"; 
    ///<summary>The language the content is in</summary> 
    public const string ContentLanguage = "Content-Language"; 
    ///<summary>The length of the response body in octets (8-bit bytes)</summary> 
    public const string ContentLength = "Content-Length"; 
    ///<summary>An alternate location for the returned data</summary> 
    public const string ContentLocation = "Content-Location"; 
    ///<summary>A Base64-encoded binary MD5 sum of the content of the response</summary> 
    public const string ContentMD5 = "Content-MD5"; 
    ///<summary>An opportunity to raise a File Download dialogue box for a known MIME type with binary format or suggest a filename for dynamic content. Quotes are necessary with special characters.</summary> 
    public const string ContentDisposition = "Content-Disposition"; 
    ///<summary>Where in a full body message this partial message belongs</summary> 
    public const string ContentRange = "Content-Range"; 
    ///<summary>The MIME type of this content</summary> 
    public const string ContentType = "Content-Type"; 
    ///<summary>The date and time that the message was sent</summary> 
    public const string Date = "Date"; 
    ///<summary>An identifier for a specific version of a resource, often a message digest</summary> 
    public const string ETag = "ETag"; 
    ///<summary>Gives the date/time after which the response is considered stale</summary> 
    public const string Expires = "Expires"; 
    ///<summary>The last modified date for the requested object, inRFC 2822 format</summary> 
    public const string LastModified = "Last-Modified"; 
    ///<summary>Used to express a typed relationship with another resource, where the relation type is defined by RFC 5988</summary> 
    public const string Link = "Link"; 
    ///<summary>Used in redirection, or when a new resource has been created.</summary> 
    public const string Location = "Location"; 
    ///<summary>This header is supposed to set P3P policy, in the form of P3P:CP=your_compact_policy. However, P3P did not take off,[22] most browsers have never fully implemented it, a lot of websites set this header with fake policy text, that was enough to fool browsers the existence of P3P policy and grant permissions for third party cookies.</summary> 
    public const string P3P = "P3P"; 
    ///<summary>Implementation-specific headers that may have various effects anywhere along the request-response chain.</summary> 
    public const string Pragma = "Pragma"; 
    ///<summary>Request authentication to access the proxy.</summary> 
    public const string ProxyAuthenticate = "Proxy-Authenticate"; 
    ///<summary>Used in redirection, or when a new resource has been created. This refresh redirects after 5 seconds. This is a proprietary, non-standard header extension introduced by Netscape and supported by most web browsers.</summary> 
    public const string Refresh = "Refresh"; 
    ///<summary>If an entity is temporarily unavailable, this instructs the client to try again after a specified period of time (seconds).</summary> 
    public const string RetryAfter = "Retry-After"; 
    ///<summary>A name for the server</summary> 
    public const string Server = "Server"; 
    ///<summary>an HTTP cookie</summary> 
    public const string SetCookie = "Set-Cookie"; 
    ///<summary>A HSTS Policy informing the HTTP client how long to cache the HTTPS only policy and whether this applies to subdomains.</summary> 
    public const string StrictTransportSecurity = "Strict-Transport-Security"; 
    ///<summary>The Trailer general field value indicates that the given set of header fields is present in the trailer of a message encoded with chunked transfer-coding.</summary> 
    public const string Trailer = "Trailer"; 
    ///<summary>The form of encoding used to safely transfer the entity to the user. Currently defined methods are:chunked, compress, deflate, gzip, identity.</summary> 
    public const string TransferEncoding = "Transfer-Encoding"; 
    ///<summary>Tells downstream proxies how to match future request headers to decide whether the cached response can be used rather than requesting a fresh one from the origin server.</summary> 
    public const string Vary = "Vary"; 
    ///<summary>Informs the client of proxies through which the response was sent.</summary> 
    public const string Via = "Via"; 
    ///<summary>A general warning about possible problems with the entity body.</summary> 
    public const string Warning = "Warning"; 
    ///<summary>Indicates the authentication scheme that should be used to access the requested entity.</summary> 
    public const string WWWAuthenticate = "WWW-Authenticate";  
} 
+0

मैंने सोचा, "निश्चित रूप से इन स्थिरांक कहीं नेट ढांचे में मौजूद होना चाहिए," लेकिन जब मैं System.Net.Http.Headers.HttpRequestHeader.IfModifiedSince पर परिलक्षित मैं बेक्ड में स्ट्रिंग लगता है "अगर - संशोधित-चूंकि "एक बार नहीं, लेकिन दो बार। – Hugh

+2

@ हूग इतना तेज़ नहीं है; कंपाइलर उस स्ट्रिंग को उस कोड में जला देगा, भले ही वे कॉन्स्ट थे, http://www.stum.de/2009/01/14/const-strings-a-very-convenient-way-to-shoot- स्वयं -इन-द-पैर/- वस्तुतः, वे कहते हैं कि वे कभी भी तारों के लिए कॉन्स्ट का उपयोग नहीं करते हैं जो संदर्भ में जलाए जाने के बाद बदल सकते हैं और अलग-अलग समय में संकलित libs के विभिन्न संस्करणों में अलग-अलग हो सकते हैं। –

6

माइक्रोसॉफ्ट अनुरोध और प्रतिक्रिया हेडर के लिए enums बनाया।

निम्नलिखित पर एक नज़र डालें:

HttpResponseHeader

HttpRequestHeader

+0

धन्यवाद जेड - कभी-कभी फ्रेमवर्क एक विशाल हार्डवेयर स्टोर के द्वीपों को ऊपर और नीचे घूमने जैसा है, और जैसा कि आप देख सकते हैं, मैं अधीर हूं;) –

+11

यह आपको स्ट्रिंग स्थिरांक प्रदान नहीं करेगा। यद्यपि आप 'HttpRequestHeader.AcceptLanguage.ToString() 'कर सकते हैं, परिणाम' स्वीकार्य भाषा 'होगा और' स्वीकार्य-भाषा 'नहीं होगा। तो मुझे नहीं लगता कि यह वास्तव में सवाल का जवाब देता है। – Anton

+0

@ एंटन - ओपी ने कभी भी उल्लेख नहीं किया कि वह स्ट्रिंग स्थिरांक की तलाश में था - इसके अलावा, उसने जो टिप्पणी पोस्ट की है (आपके ऊपर से ऊपर) का तात्पर्य है कि एमएस enums पर्याप्त है। – Jed

4

जेड के जवाब पर विस्तार करने के लिए।

HttpResponseHeader और HttpRequestHeader गणना WebHeaderCollection का उपयोग करते समय आपके स्थिरांक के रूप में उपयोग की जा सकती है। WebHeaderCollection में इंडेक्सर गुण हैं जो इन गणनाओं को स्वीकार करते हैं।

आप हेडर वैल्यू प्राप्त करने और सेट करने के लिए या तो एक स्ट्रिंग या गणनाओं में से एक का उपयोग कर सकते हैं, और इसे कोड के साथ भी मिश्रण कर सकते हैं।

उदाहरण LINQPad स्क्रिप्ट:

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(); 
+0

इन enums में पूर्णांक मान हैं। उन पर '.ToString()' को कॉल करने का एकमात्र विकल्प है, या स्ट्रिंग के रूप में नाम पाने का कोई और तरीका है? – crush

+1

@ क्रश मैं मूल्य की स्ट्रिंग प्रस्तुति प्राप्त करने के लिए 'ToString() 'का उपयोग नहीं करता, क्योंकि कई शीर्षलेख मानों में एक हाइफ़न होता है जिसे आप इस तरीके से नहीं प्राप्त करेंगे। –

+0

धन्यवाद, यह एक अच्छा मुद्दा है जिसके बारे में मैंने नहीं सोचा था। मैं एमवीसी में एक प्रतिक्रिया वस्तु पर शीर्षलेख सेट करने की कोशिश कर रहा हूँ। 'System.Web.HttpResponseBase' संस्करण 4.0.0.0। क्या कोई ऐसा enum है जिसमें तारों का उपयोग किया जा सकता है, या मुझे खुद को एक सहायक वर्ग बनाने की आवश्यकता होगी? – crush

4

मुझे एक ही चीज़ खोजने की कोशिश करते समय यह प्रश्न मिला: शीर्षलेख नाम स्थिरांक तारों के रूप में कहां हैं?

Microsoft.Net.Http.Headers.HeaderNames वह वर्ग है जो मुझे बचाता है।

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"; 
    } 
+0

ऐसी कोई कक्षा नहीं है, क्या आप संदर्भ दे सकते हैं? –

+0

निश्चित रूप से, यदि आप AspNetCore का उपयोग कर रहे हैं। जैसा कि मैंने कहा, यह मेरी मदद करता है और एक ही समस्या के साथ दूसरों की मदद कर सकता है। कोड के लिए https://github.com/aspnet/AspLabs/blob/master/src/Microsoft.AspNetCore/Microsoft.Net.Http.Headers.cs, या एमएसडीएन संदर्भ https://docs.microsoft.com/ है en-us/dotnet/api/microsoft.net.http.headers? view = aspnetcore-2.0 – Mashton

+0

ठीक है मैं 4.5 का उपयोग कर रहा हूं और मूल नहीं: p –

संबंधित मुद्दे