Asp.net mvc MVC4-显示有关错误的更多信息
我们正在使用“HttpResponseExpection”抛出异常消息。在异常中,尝试显示错误内容和原因短语,但仅显示错误状态代码。 发布了我用来显示消息的代码Asp.net mvc MVC4-显示有关错误的更多信息,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我们正在使用“HttpResponseExpection”抛出异常消息。在异常中,尝试显示错误内容和原因短语,但仅显示错误状态代码。 发布了我用来显示消息的代码 var resp = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent(string.Format("No Product w
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(string.Format("No Product with ID = {0}", id)),
ReasonPhrase = "Product ID Not Found",
StatusCode = HttpStatusCode.Forbidden
}; throw new HttpResponseException(resp);
来自index.cshtml的代码
function find() {
clearStatus();
var id = $('#productId').val();
$.getJSON(API_URL + id,
function (data) {
viewModel.Name(data.Name);
viewModel.Category(data.Category);
viewModel.Price(data.Price);
})
.fail(
function (jqXHR, textStatus, err) {
$('#status').html('Error: ' + err );
});
}
非常感谢您的帮助。
jqXHR
对象的responseText
属性将包含错误消息(Content
属性):
如果要获取ReasonPhrase
属性,请使用err
参数:
.fail(function (jqXHR, textStatus, err) {
alert(err);
});
还要确保在API控制器上使用的是正确的状态代码404(HttpStatusCode.NotFound
),而不是403(HttpStatusCode.Forbidden
):
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(string.Format("No Product with ID = {0}", id)),
ReasonPhrase = "Product ID Not Found",
StatusCode = HttpStatusCode.Forbidden
};
throw new HttpResponseException(resp);
jqXHR
对象的responseText
属性将包含错误消息(Content
属性):
如果要获取ReasonPhrase
属性,请使用err
参数:
.fail(function (jqXHR, textStatus, err) {
alert(err);
});
还要确保在API控制器上使用的是正确的状态代码404(HttpStatusCode.NotFound
),而不是403(HttpStatusCode.Forbidden
):
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(string.Format("No Product with ID = {0}", id)),
ReasonPhrase = "Product ID Not Found",
StatusCode = HttpStatusCode.Forbidden
};
throw new HttpResponseException(resp);
我搞不懂你的问题是什么。