问题
I have made an Azure Mobile Service which works fine locally.
I then publish the Azure mobile Service to the cloud and the website shows that it is currently running.
I then make a GET Request
to my published website and it returns the following:
Status 500 Internal Server Error 500 Internal Server Error A generic error message, given when no more specific message is suitable
{ "message": "An error has occurred." }
I have tried adding:
<system.web>
<customErrors mode="Off"/>
</system.web>
To my Web.Config
in the Site\wwwroot
directory but this didn't do anything.
So how do I return a more meaningful error from my azure mobile service?
回答1:
So turns out returning errors from a Web API
is different to returning errors from Asp.net
or iis
From this link I found the following:
Today Kurt and I were attempting to debug an Web API service we had deployed to a remote machine. The service was returning 500 errors, and for various reasons, we couldn’t just try to do the requests from that deployed box. We wanted to get the full exception details in the response, but we were just seeing blank 500 errors, with no responses.
We first tried the Web.Config setting for custom errors:
<customErrors mode="Off" />
But this didn’t affect anything. Digging a little further, we found that ASP.NET Web API uses a different configuration for error details being passed along. This is for a couple of reasons; first, the custom errors element in the Web.Config is an ASP.NET thing. It’s something that ASP.NET uses to determine if that yellow screen of death with additional detail should be shown to users. However, ASP.NET Web API is designed to be self-hosted, outside of ASP.NET and IIS. While the customErrors element affects requests for ASPX and MVC, it does nothing for Web API.
Instead of relying on a lot of XML configuration, Web API uses a lot of programmatic configuration. This helps self hosting, but for changing policies like error detail, we have to change the code, re-compile and re-deploy. To set the error policy in our application, we need to modify our global Web API configuration:
GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
With this mode, requests from any source will get us full exception detail. It’s likely not something we want in production, but nice that it is available.
So in conclusion to view errors from a Web API
you want to go to your Startup
class where you will see a ConfigureMobileApp
method.
This will include the line:
HttpConfiguration config = new HttpConfiguration();
and you want to add the following:
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
回答2:
The Azure Mobile Service handles all exceptions and raises an HttpResponseException with generic error message, if you want to get the detailed error message yo can do something like this:
// POST tables/TodoItem
public async Task<IHttpActionResult> PostTodoItem(TodoItem item)
{
try
{
TodoItem current = await InsertAsync(item);
return CreatedAtRoute("Tables", new { id = current.Id }, current);
}
catch (HttpResponseException ex)
{
string message = ((HttpError)((ObjectContent)ex.Response.Content).Value).First().Value.ToString();
string[] temp = message.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
var resp = new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent(message),
ReasonPhrase = temp[0]
};
throw new HttpResponseException(resp);
}
}
This article point me in the right direction
来源:https://stackoverflow.com/questions/35010399/how-do-i-return-an-exception-from-azure-mobile-services