This means that at some point your code is catching this exception and handling it. This would be something such as a try catch, or if you have handled the Application_Error in the global.asax.
You might want to look into returning HttpResponseMessage<T>. Then you can specify the status code without throwing an exception.
example
return new HttpResponseMessage<MyModel>(model, HttpStatusCode.NotFound);
I seem to remember that yesterday you wrote: "Http Exception handled by User Code". Your original post now reads "Http Exception
Un handled by User Code". You've edited it, is that correct?
If so, and if your problem is that the exception is unhandled by user code, then that is perfectly understandable. And the good news is that there is no problem.
You are throwing an exception - correctly - that will go outside of your "user code" and which will be handled by the framework to generate the correct HTTP response to the client.
Because you don't want to be warned about this, do the following (which is what I do):-
Open Visual Studio
Load your project
Click on main menu item Debug > Exceptions...
In the dialog box, click on Add...
In the Type dropdown select "Common Language Runtime Exceptions"
In the Name textbox type in "System.Web.Http.HttpResponseException" (without the quotes)
Click on OK
In the list of exceptions, locate the one you just added, and ensure that both checkboxes are unchecked.
Click on OK.
Now your code can legitmately throw this exception out of your user code, and VS will not break.
dilbaghrangi
Member
4 Points
3 Posts
web api - HttpResponse Exception
Mar 15, 2012 11:17 AM|LINK
hi ,
In query funcion if a record is null than i am thorwing the exception i.e
throw new HttpResponseException(HttpStatusCode.NotFound);
but when i run the aplication. than it point out that "HttpResposeException was Unhandled by user code"
so please let me know what is the issue with this code..
Code :
public IQueryable<Identity> QueryEddaIdentities(QueryEddaReq request)
{var customerDetail = _securityInstance.QueryEddaIdentities(request);
if (customerDetail == null)
throw new HttpResponseException
(new HttpResponseMessage{StatusCode =HttpStatusCode.NotFound,Content =new StringContent("Identites not found"),ReasonPhrase ="Identites not found"});
return customerDetail.AsQueryable();
}
ANd Use the following ExceptionHandler Filter
public class ExceptionHandlerFilter :ExceptionFilterAttribute
{publicExceptionHandlerFilter(){this.Mappings = new Dictionary<Type, HttpStatusCode>();this.Mappings.Add(typeof(ArgumentNullException), HttpStatusCode.BadRequest);this.Mappings.Add(typeof(ArgumentException), HttpStatusCode.BadRequest);}
public IDictionary<Type, HttpStatusCode> Mappings { get; private set; }
public override void OnException(HttpActionExecutedContext(actionExecutedContext)
{if (actionExecutedContext.Exception != null)
{var exception = actionExecutedContext.Exception;
if (actionExecutedContext.Exception is HttpException)
{var httpException = (HttpException)exception;actionExecutedContext.Result =new HttpResponseMessage{Content = new StringContent( exception.Message ),StatusCode = (HttpStatusCode)httpException.GetHttpCode()};
}
else if (this.Mappings.ContainsKey(exception.GetType()))
{
var httpStatusCode = this.Mappings[exception.GetType()];
actionExecutedContext.Result =new HttpResponseMessage{Content = new StringContent( exception.Message ),StatusCode = httpStatusCode};
}
else{actionExecutedContext.Result =new HttpResponseMessage { Content = new StringContent(exception.Message), StatusCode = HttpStatusCode.InternalServerError };}}}}
So whan i throw the httpException than this filter does not handle it.So my app gives the error "Http Exception Un handled by User Code"
stuie
Participant
1467 Points
288 Posts
Re: web api - HttpResponse Exception
Mar 15, 2012 11:20 AM|LINK
Hi,
This means that at some point your code is catching this exception and handling it. This would be something such as a try catch, or if you have handled the Application_Error in the global.asax.
You might want to look into returning HttpResponseMessage<T>. Then you can specify the status code without throwing an exception.
example
return new HttpResponseMessage<MyModel>(model, HttpStatusCode.NotFound);dilbaghrangi
Member
4 Points
3 Posts
Re: web api - HttpResponse Exception
Mar 16, 2012 06:38 AM|LINK
Thanks for Reply : But i am using Iqueryable as return type. thats why i have not used httpresonsemessage.
Code :
<div class="comment-right-col"> <div>public IQueryable<Identity> QueryEddaIdentities(QueryEddaReq request)
{var customerDetail = _securityInstance.QueryEddaIdentities(request);
if (customerDetail == null)
throw new HttpResponseException
(new HttpResponseMessage{StatusCode =HttpStatusCode.NotFound,Content =new StringContent("Identites not found"),ReasonPhrase ="Identites not found"});
return customerDetail.AsQueryable();
}
ANd Use the following ExceptionHandler Filter
public class ExceptionHandlerFilter :ExceptionFilterAttribute
{publicExceptionHandlerFilter(){this.Mappings = new Dictionary<Type, HttpStatusCode>();this.Mappings.Add(typeof(ArgumentNullException), HttpStatusCode.BadRequest);this.Mappings.Add(typeof(ArgumentException), HttpStatusCode.BadRequest);}
public IDictionary<Type, HttpStatusCode> Mappings { get; private set; }
public override void OnException(HttpActionExecutedContext(actionExecutedContext)
{if (actionExecutedContext.Exception != null)
{var exception = actionExecutedContext.Exception;
if (actionExecutedContext.Exception is HttpException)
{var httpException = (HttpException)exception;actionExecutedContext.Result =new HttpResponseMessage{Content = new StringContent( exception.Message ),StatusCode = (HttpStatusCode)httpException.GetHttpCode()};
}
else if (this.Mappings.ContainsKey(exception.GetType()))
{
var httpStatusCode = this.Mappings[exception.GetType()];
actionExecutedContext.Result =new HttpResponseMessage{Content = new StringContent( exception.Message ),StatusCode = httpStatusCode};
}
else{actionExecutedContext.Result =new HttpResponseMessage { Content = new StringContent(exception.Message), StatusCode = HttpStatusCode.InternalServerError };}}}}
So whan i throw the httpException than this filter does not handle it.So my app gives the error "Http Exception Un handled by User Code"
</div> </div>awebb
Member
204 Points
91 Posts
Re: web api - HttpResponse Exception
Mar 16, 2012 08:27 AM|LINK
I seem to remember that yesterday you wrote: "Http Exception handled by User Code". Your original post now reads "Http Exception Un handled by User Code". You've edited it, is that correct?
If so, and if your problem is that the exception is unhandled by user code, then that is perfectly understandable. And the good news is that there is no problem.
You are throwing an exception - correctly - that will go outside of your "user code" and which will be handled by the framework to generate the correct HTTP response to the client.
Because you don't want to be warned about this, do the following (which is what I do):-
Now your code can legitmately throw this exception out of your user code, and VS will not break.
dilbaghrangi
Member
4 Points
3 Posts
Re: web api - HttpResponse Exception
Mar 16, 2012 09:17 AM|LINK
Hi awebb,
Thank you very much...