Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

400 status error handling with Web API 2 and Forms application in same project - how to setup to handle both?

$
0
0

 I have a project that is both has both a Web API 2 service and aspxforms.  But I am trying to set up error handling so I can send users to a 404 error page if they try putting in fake page in the url. Of course the Web API also needs to send 400 status message back to the users who call the web service so I am trying to keep the functionality of both in place. The main problem is I am not sure how to best set up the web.config as it not working. In my project I have both an error.aspx and error404.aspx pages.
 
With this in the web.config file:

<customErrors mode="RemoteOnly" defaultRedirect="~/error.aspx" redirectMode="ResponseRewrite"><error statusCode="404" redirect="~/error404.aspx"/></customErrors><!-- setting existingResponse="PassThrough" needed, otherwise web api 400 error messages will not return --><httpErrors existingResponse="PassThrough" errorMode="Custom"><remove statusCode="404" subStatusCode="-1"/><error statusCode="404" prefixLanguageFilePath="" path="/myApp/error404.aspx" responseMode="ExecuteURL"/></httpErrors>

   
My app will return something like the following when I put in a fake page: http://myServer/myApp/fake.aspx

 Server Error in '/myApp' Application.
Runtime Error
Description: An exception occurred while processing your request. Additionally, another exception occurred while executing the custom error page for the first exception. The request has been terminated.     

However, I can get the 404 page to show up if I change in the web.config: <httpErrors existingResponse="Replace"...
But making this changes causes the web API 400 status code messages not to show up when I send something like the following from
my controller's get function: Return BadRequest("Your parameter is invalid")
    
Any ideas how I can set up the project so I can get both errors to show up? Setting<httpErrors existingResponse="Auto"...
allows the Web API errors to be sent back, but not the error404.aspx for bad forms requests.   

Thanks for your time.


Viewing all articles
Browse latest Browse all 4850