Azure 不在 HttpException 中传递我的自定义消息
Azure doesn't pass my custom message in HttpException
我在 Azure WebApp 中有一个 REST API。
当 POST 发送到我的端点时,我会进行一些检查,如果需要,我会抛出一个 HttpException:
throw new HttpException(400, msgInfo);
其中 msgInfo
是我的自定义消息。在我使用 Visual Studio 2015 的开发机器中,我的回答是:
{"Message":"An error has occurred.","ExceptionMessage":"[my custom message]","ExceptionType":"System.Web.HttpException","StackTrace":"..."}
现在我可以向用户显示有用的消息了。
但在 Azure 上,响应只是:
{"Message":"An error has occurred."}
所以没有自定义消息。
这很可能是 Azure 中的设置。我知道它不应该显示我的完整堆栈跟踪,但它应该显示 ExceptionMessage
.
在我的 Web.config
我有:
<system.web>
<customErrors mode="RemoteOnly" />
</system.web>
<system.webServer>
<httpErrors errorMode="Detailed" />
</system.webServer>
如何解决这个问题?
Asp.net web api 有一个单独的配置,用于如何在不同的环境中显示错误详细信息。
在你HttpConfiguration
里面,有一个属性叫IncludeErrorDetailPolicy
。这是它的可能值。
public enum IncludeErrorDetailPolicy
{
// Summary:
// Use the default behavior for the host environment. For ASP.NET hosting, usethe value from the customErrors element in the Web.config file.
// For self-hosting, use the value System.Web.Http.IncludeErrorDetailPolicy.LocalOnly.
Default = 0,
// Summary:
// Only include error details when responding to a local request.
LocalOnly = 1,
//
// Summary:
// Always include error details.
Always = 2,
//
// Summary:
// Never include error details.
Never = 3,
}
您可以配置如下:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCloudServiceGateway();
var config = new HttpConfiguration
{
IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always // Add this line to enable detail mode in release
};
WebApiConfig.Register(config);
app.UseWebApi(config);
}
}
更多细节,你可以参考这个thread。
另外,您可以设置 <customErrors mode="Off"/>
,指定禁用自定义错误。 detailed ASP.NET errors 显示给远程客户端和本地主机。
我在 Azure WebApp 中有一个 REST API。 当 POST 发送到我的端点时,我会进行一些检查,如果需要,我会抛出一个 HttpException:
throw new HttpException(400, msgInfo);
其中 msgInfo
是我的自定义消息。在我使用 Visual Studio 2015 的开发机器中,我的回答是:
{"Message":"An error has occurred.","ExceptionMessage":"[my custom message]","ExceptionType":"System.Web.HttpException","StackTrace":"..."}
现在我可以向用户显示有用的消息了。
但在 Azure 上,响应只是:
{"Message":"An error has occurred."}
所以没有自定义消息。
这很可能是 Azure 中的设置。我知道它不应该显示我的完整堆栈跟踪,但它应该显示 ExceptionMessage
.
在我的 Web.config
我有:
<system.web>
<customErrors mode="RemoteOnly" />
</system.web>
<system.webServer>
<httpErrors errorMode="Detailed" />
</system.webServer>
如何解决这个问题?
Asp.net web api 有一个单独的配置,用于如何在不同的环境中显示错误详细信息。
在你HttpConfiguration
里面,有一个属性叫IncludeErrorDetailPolicy
。这是它的可能值。
public enum IncludeErrorDetailPolicy
{
// Summary:
// Use the default behavior for the host environment. For ASP.NET hosting, usethe value from the customErrors element in the Web.config file.
// For self-hosting, use the value System.Web.Http.IncludeErrorDetailPolicy.LocalOnly.
Default = 0,
// Summary:
// Only include error details when responding to a local request.
LocalOnly = 1,
//
// Summary:
// Always include error details.
Always = 2,
//
// Summary:
// Never include error details.
Never = 3,
}
您可以配置如下:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCloudServiceGateway();
var config = new HttpConfiguration
{
IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always // Add this line to enable detail mode in release
};
WebApiConfig.Register(config);
app.UseWebApi(config);
}
}
更多细节,你可以参考这个thread。
另外,您可以设置 <customErrors mode="Off"/>
,指定禁用自定义错误。 detailed ASP.NET errors 显示给远程客户端和本地主机。