开发者

Get description for HTTP status code

开发者 https://www.devze.com 2023-01-12 20:16 出处:网络
In ASP.NET you can set the Response.StatusCode to for example 404. Should the status line / description always be set? (to in this cas开发者_运维技巧e \"404 Page Not Found\")

In ASP.NET you can set the Response.StatusCode to for example 404. Should the status line / description always be set? (to in this cas开发者_运维技巧e "404 Page Not Found")

How do you get the description if you only have the code (404)? Is this somewhere in the framework or do you manually have to hardcode the descriptions?


You can use the static method HttpWorkerRequest.GetStatusDescription for this.


If you need it at the same time you're pulling Response.StatusCode, you can get the description from Response.StatusDescription.


The status description can be retrieved with some crazy type casting. Here is the code snipped which retrieves the custom exception message (this is client side code only)

try
{
    string exText = ((HttpWebResponse)w.Response).StatusDescription);
}
catch (WebException w)
{    
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号