问题
In ASP.NET you can set the Response.StatusCode to for example 404. Should the status line / description always be set? (to in this case "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?
回答1:
You can use the static method HttpWorkerRequest.GetStatusDescription for this.
回答2:
If you need it at the same time you're pulling Response.StatusCode, you can get the description from Response.StatusDescription.
回答3:
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)
{
}
来源:https://stackoverflow.com/questions/3542870/get-description-for-http-status-code