According to Mike Wasson's article about exception-handling , I test it in my controller . Code is below:
public class ValuesController : ApiController
{
// GET /api/<controller>
public IEnumerable<string> Get()
{
var response = new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
response.Content = new StringContent("my test content");
response.ReasonPhrase = "some reason";
throw new HttpResponseException(response);
}
//....
}
When I access it in IE, server return 401 error, but it seemed that the content was replaced to default 401 page by iis.
If I remove "System.Net.HttpStatusCode.Unauthorized" off, Server return 200 ok and custom content is shown .
My server run windows server 2008 and IIS7 . Need to write something in application'web.config file for IIS?
I want the application can return more meaningful error messages, what should I do ?
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Marked as answer by wypwgx on Apr 19, 2012 06:40 AM
I set "TrySkipIISCustionError = true" before throw HttpReponseException, Now it works.
In client I use jQuery .ajax() to access the api. In ajax error(jqXHR, textStatus, errorThrown) method, it show that the value of errorThrown is the value of HttpResponseMessage.ReasonPhrase, and jqXHR.responseText start with the custom content which I
set to HttpResponseMessage.Content.
But, behind custom content , jqXHR.responseText also include html content of "Server Error in '/' Application - Runtime Error" .
My question is How can remove that html content?
Another hand, if I write <httpErrors existingResponse="PassThrough" /> to web.config in spite of "TrySkipIISCustionError" be set or not, server always return 500 error and custom content not shown.
var response
=newHttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
Forms Authentication Module is coming into the middle. For these tasks,
AuthorizationFilterAttribute is better place.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Marked as answer by wypwgx on Apr 19, 2012 06:40 AM
wypwgx
Member
16 Points
10 Posts
How can return more meaningful error message?
Apr 18, 2012 02:40 PM|LINK
According to Mike Wasson's article about exception-handling , I test it in my controller . Code is below:
public class ValuesController : ApiController { // GET /api/<controller> public IEnumerable<string> Get() { var response = new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized); response.Content = new StringContent("my test content"); response.ReasonPhrase = "some reason"; throw new HttpResponseException(response); } //.... }When I access it in IE, server return 401 error, but it seemed that the content was replaced to default 401 page by iis.
If I remove "System.Net.HttpStatusCode.Unauthorized" off, Server return 200 ok and custom content is shown .
My server run windows server 2008 and IIS7 . Need to write something in application'web.config file for IIS?
I want the application can return more meaningful error messages, what should I do ?
imran_ku07
All-Star
45864 Points
7713 Posts
MVP
Re: How can return more meaningful error message?
Apr 18, 2012 03:10 PM|LINK
use TrySkipIISCustomError, See this
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
wypwgx
Member
16 Points
10 Posts
Re: How can return more meaningful error message?
Apr 19, 2012 03:41 AM|LINK
Thanks, imran_ku07.
I set "TrySkipIISCustionError = true" before throw HttpReponseException, Now it works.
In client I use jQuery .ajax() to access the api. In ajax error(jqXHR, textStatus, errorThrown) method, it show that the value of errorThrown is the value of HttpResponseMessage.ReasonPhrase, and jqXHR.responseText start with the custom content which I set to HttpResponseMessage.Content.
But, behind custom content , jqXHR.responseText also include html content of "Server Error in '/' Application - Runtime Error" .
My question is How can remove that html content?
Another hand, if I write <httpErrors existingResponse="PassThrough" /> to web.config in spite of "TrySkipIISCustionError" be set or not, server always return 500 error and custom content not shown.
imran_ku07
All-Star
45864 Points
7713 Posts
MVP
Re: How can return more meaningful error message?
Apr 19, 2012 06:06 AM|LINK
Forms Authentication Module is coming into the middle. For these tasks, AuthorizationFilterAttribute is better place.
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
wypwgx
Member
16 Points
10 Posts
Re: How can return more meaningful error message?
Apr 19, 2012 06:40 AM|LINK
Now it works perfectly, Thanks!