简体   繁体   English

ASP.NET MVC-JSON + HttpException

[英]ASP.NET MVC - JSON + HttpException

I'm trying to return an error page indicating that the user couldnt be found and therefore I throw a HttpException with status code 404, however for some reason it wont redirect to the error page? 我试图返回一个错误页面,指示找不到该用户,因此我抛出了状态代码为404的HttpException,但是由于某种原因,它不会重定向到错误页面? - Is there some sort of trick to handle error pages with JSON that I should be aware of? -是否有某种技巧可用来处理带有JSON的错误页面?

My current code: 我当前的代码:

public ActionResult Details(int id)
{
    User userToGet = _session.Query(new GetUserById(id));

    if(userToGet == null)
    {
        throw new HttpException(404, "User not found");
    }

    DetailsUserViewModel userToViewModel = AutoMapper.Mapper.Map<User, DetailsUserViewModel>(userToGet);

    return Json(new
    {
        HTML = RenderPartialViewToString("Partials/Details", userToViewModel)
    }, JsonRequestBehavior.AllowGet);
}

Thanks in advance! 提前致谢!

Update - Some more code: 更新-更多代码:

// MyJs.js // MyJs.js

function openBox(object) {
    $("body").append("<div id='fadeBox'></div><div id='overlay' style='display:none;'></div>");
    $("#fadeBox").html("<div style='position:absolute;top:0;right:0;margin-right:2px;margin-top:2px;'><a id='closeBox' href='#'>x</a></div>" + object["HTML"])
    $("#fadeBox").fadeIn("slow");
    $('#wrapper').attr('disabled', 'disabled');
    $("#overlay").show();
    jQuery.validator.unobtrusive.parse('#mybox') /* Enable client-side validation for partial view */
}

// List.cshtml // List.cshtml

@model IEnumerable<MyDemon.ViewModels.ListUsersViewModel>

<table style="width:100%;">
    <tr style="font-weight:bold;">
        <td>UserId</td>
        <td>UserName</td>
    </tr>
    @foreach (var user in Model)
    {
        <tr>
            <td>@user.UserId</td>
            <td>@user.UserName</td>
            <td>@Ajax.ActionLink("Details", "details", "account", new { id = @user.UserId }, new AjaxOptions() { HttpMethod = "GET", OnSuccess = "openBox" })</td>
        </tr>
    }
</table>

Your controller action is very strange. 您的控制器动作非常奇怪。 You are returning a JSON object but which contains HTML. 您将返回一个JSON对象,但其中包含HTML。 That's not necessary. 没必要 If you intend to return HTML then return a partial view. 如果您打算返回HTML,则返回部分视图。 JSON brings no value in your situation: JSON在您的情况下没有任何价值:

public ActionResult Details(int id)
{
    User userToGet = _session.Query(new GetUserById(id));

    if(userToGet == null)
    {
        return PartialView("404");
    }

    DetailsUserViewModel userToViewModel = AutoMapper.Mapper.Map<User, DetailsUserViewModel>(userToGet);

    return PartialView("~/Views/Home/Partials/Details.ascx", userToViewModel);
}

If you want to use JSON then work with objects: 如果要使用JSON,请使用对象:

public ActionResult Details(int id)
{
    User userToGet = _session.Query(new GetUserById(id));

    if(userToGet == null)
    {
        return Json(new { user = null }, JsonRequestBehavior.AllowGet);
    }

    DetailsUserViewModel userToViewModel = AutoMapper.Mapper.Map<User, DetailsUserViewModel>(userToGet);

    return Json(new { user = userToViewModel }, JsonRequestBehavior.AllowGet);
}

and then: 接着:

$.getJSON('<%= Url.Action("Details") %>', { id: '123' }, function(result) {
    if (result.user == null) {
        alert('user not found');
    } else {
        // do something with result.user
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM