关于ASP.net MVC中的Ajax请求中的错误处理的疑问

时间:2022-11-04 04:15:10

I have been developing a web application on my local machine for some days now using Visual Studio 2010. Last night I deployed my application on IIS 7 and frustratingly stumbled across a major show stopper. The problem is related to error handling in Ajax request. To begin with I have some edit screens which works on ajax request. Here is the what the ajax part of the Ajax edit screen looks like.

我使用Visual Studio 2010在本地机器上开发web应用程序已经有几天了。昨晚,我在iis7上部署了我的应用程序,令人沮丧的是,我偶然发现了一个主要的show stopper。这个问题与Ajax请求中的错误处理有关。首先,我有一些编辑屏幕,可以处理ajax请求。以下是ajax编辑屏幕的ajax部分。

@using (Ajax.BeginForm("_CityEdit", null, new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "content", 
                                                            LoadingElementId="imgSaveLoading", OnFailure="onError",
                                                            OnSuccess = "onSuccess" }, new { id = "frmCity" })) {
    <img id="imgSaveLoading" alt="loading" src="@Url.Content("~/Content/Telerik/Vista/loading.gif")" class="ajax-loader-user" />
    <div class="content" id="content">
        @{Html.RenderPartial("_CityEdit");}   
    </div>

    <!-- Some Other code here -->
}

I was relying on the "onError" callback of the above Ajax.BeginForm(). So in my controller, whenever I wanted to display error to user (model error or any other error), I just set the Server Response to 500 (Internal Server Error) optimistically hoping that this would result in "OnError" handler of Ajax.BeginForm() being invoked. Here is a snippet of how I did this in my controller.

我依赖于上述Ajax.BeginForm()的“onError”回调。因此,在我的控制器中,每当我想向用户显示错误(模型错误或任何其他错误)时,我就乐观地将服务器响应设置为500(内部服务器错误),希望这会导致调用Ajax.BeginForm()的“OnError”处理程序。下面是我如何在我的控制器中执行此操作的一个片段。

[HttpPost]
public ActionResult _CityEdit(CityViewModel viewModel) {
    city = cityManager.FindCity(viewModel.City.Id, false);
    // Validate Model here 
    if (!TryUpdateModel(city, "City")) {
        Response.StatusCode = 500;
        return PartialView("_CityEdit", viewModel);
    }

As can be seen from above code, I returned PartialView and if it contained Model Errors, they will automatically get displayed on the Ajax Edit Screen. Then in the "OnError" JavaScript I just replace the related div with the PartialView returned by the controller something like this.

从上面的代码可以看出,我返回了PartialView,如果其中包含模型错误,它们将自动显示在Ajax编辑屏幕上。然后在“OnError”JavaScript中,我用控制器返回的PartialView替换相关的div。

function onError(xhr) {
    $('#content').html(xhr.responseText);
}

This all worked great on my development machine. When I deployed my application on IIS 7 and accessed it from an windows XP box (with IE 8 or Chrome), instead of PartialView, the browsers just displayed the default "500 Internal Server Error" screen. So I have a few doubts that caused me not to sleep well last night.

这在我的开发机器上运行得很好。当我将我的应用程序部署到iis7上并从windows XP系统(IE 8或Chrome)中访问它时,浏览器没有显示PartialView,而是显示默认的“500内部服务器错误”屏幕。所以我有一些疑问,导致我昨晚睡不好。

  1. Is the approach that I have used for Ajax editing a reasonable one. If not what could be the other way(s) I could have handled Ajax Editing (with ability to display model errors).

    我用于Ajax编辑的方法是合理的。如果不是,我可以用其他方式处理Ajax编辑(能够显示模型错误)。

  2. Since I been developing these modules for some days now and have put up considerable effort, is there any work arounds to get it working on XP box. Basically, it would be ideal if I could somehow prevent browser from displaying their default "500 Internal Server Error" screen.

    由于我已经开发这些模块几天了,并付出了相当大的努力,是否有任何工作可以让它在XP box上工作。基本上,如果我能以某种方式阻止浏览器显示默认的“500个内部服务器错误”屏幕,那就太理想了。

  3. Is this approach working on windows 7, because I have IIS 7 installed on the same windows 7 machine that I am using to browse to site, or its just that it works well with windows 7.

    这种方法是否适用于windows 7,因为我在windows 7机器上安装了iis7,而我正在使用它浏览站点,或者仅仅是因为它适用于windows 7。

If any other suggestions, please go ahead.

如果还有什么建议,请提出来。

Edit: I have found the workaround for my problem. So that means doubt no 2 and 3 are answered. Basically the following entry in web.config prevents the browser from showing up default browser pages for the website.

编辑:我找到了解决问题的方法。这就意味着对2和3的怀疑得到了回答。以下是web中的基本条目。配置防止浏览器显示网站的默认浏览器页面。

  <system.webServer>
      <httpErrors errorMode="Detailed"/>
  </system.webServer>

But that still leaves out my first doubt. Have I used a reasonable approach to Ajax editing ?

但这仍然排除了我的第一个疑问。我是否使用了一种合理的Ajax编辑方法?


I checked the response with the fiddler and it was IIS that was sending default error page for the Server error 500. So it needs to be configured to return "Detailed" response. We can do this using the IIS manager. Another option that I was following is to define it at application level in application's web.config file.

我用fiddler检查了响应,是IIS发送服务器错误500的默认错误页面。因此,需要将它配置为返回“详细”响应。我们可以使用IIS管理器来实现这一点。我所遵循的另一个选项是在应用程序的web中定义它。配置文件。

However, I am again in a dilemma whether to go with this approach or to rework my whole application. The problem that occurs is that if there is really some module failing with 500 Internal Server, my code gets exposed in the browser as the errorMode is set to "Detailed". So I need some way to provide my custom response when I set response error code to 500 and if any other part of application fails (I am not the one setting 500 status code) then the default error page should be sent instead of exposing my code through "Detailed" response. Any help please.

然而,我又一次陷入了两难境地:是采用这种方法,还是重新设计整个应用程序。出现的问题是,如果在500个内部服务器上确实有一些模块失败,我的代码将在浏览器中公开,因为errorMode被设置为“details”。所以我需要一些方法来提供定制响应当我设置响应错误代码500,如果任何其他的应用程序失败(我不是一个设置500状态码)然后默认错误页面应该发送而不是暴露我的代码通过“详细”的反应。任何帮助请。

regards, NIrvan.

问候,NIrvan。

1 个解决方案

#1


0  

New in IIS 7: you must set HttpResponse.TrySkipIisCustomErrors to true in order to override IIS's Error Pages settings if your web.config's customErrors are set to on (or remoteOnly and you are remote.) See this answer

IIS 7中的新功能:必须设置HttpResponse。TrySkipIisCustomErrors to true,以便在web时覆盖IIS的错误页面设置。配置的定制错误设置为on(或remoteOnly,您是远程的)。看到这个答案

#1


0  

New in IIS 7: you must set HttpResponse.TrySkipIisCustomErrors to true in order to override IIS's Error Pages settings if your web.config's customErrors are set to on (or remoteOnly and you are remote.) See this answer

IIS 7中的新功能:必须设置HttpResponse。TrySkipIisCustomErrors to true,以便在web时覆盖IIS的错误页面设置。配置的定制错误设置为on(或remoteOnly,您是远程的)。看到这个答案