开发者

Get description for HTTP status code

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜