有关疑虑错误在ASP.net MVC的Ajax请求处理 [英] Doubts relating to error handling in Ajax requests in ASP.net MVC

查看:92
本文介绍了有关疑虑错误在ASP.net MVC的Ajax请求处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发我的本地机器的Web应用程序的一些日子,现在使用Visual Studio 2010。昨天晚上我部署我的应用程序在IIS 7和横跨各大秀塞令人沮丧栽了跟头。该问题与错误处理Ajax请求。首先我有一些编辑屏幕这适用于Ajax请求。这里是什么阿贾克斯编辑画面的AJAX部分的样子。

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.

@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 -->
}

我是依靠上面Ajax.BeginForm的onError的回调()。所以,在我的控制器,每当我想显示错误给用户(型号错误或任何其他错误),我只是设置了服务器响应500(内部服务器错误)乐观地希望这将导致Ajax.BeginForm的的OnError处理器()被调用。这里是我在我的控制器这样做的一个片段。

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.

[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);
    }

从上面code可以看出,我回到PartialView,如果它包含模型误差,他们将自动获得对Ajax的编辑屏幕显示。然后在的OnError的JavaScript我刚由控制器这样的事情返回PartialView更换相关的股利。

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.

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

这一切都在我的开发机器伟大的工作。而不是PartialView当我部署在IIS 7我的应用程序,从Windows XP中盒(带IE 8或铬)访问它,浏览器只显示默认的500内部服务器错误屏幕。所以,我有几个疑问,使我不至昨晚睡得好吗。

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.

  1. 是,我已经用了阿贾克斯编辑一个合理的办法。如果不是这可能是另一种方式(S)我可以处理Ajax的编辑(与显示模型错误的能力)。

  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).

由于我在开发这些模块的一些日子现在已经提出了相当大的努力,有没有变通得到它的工作在XP中。基本上,这将是理想的,如果我能以某种方式从$显示其默认500内部服务器错误画面p $ pvent浏览器。

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.

这是方法工作在Windows 7上,因为我有IIS 7安装在同一台Windows 7机器,我用来浏览网站,或者它只是它可以与Windows 7操作系统。

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.

如果任何其他建议,请继续。

If any other suggestions, please go ahead.

编辑: 我已经找到了解决办法我的问题。因此,这意味着没有疑问2和3被回答。基本上在web.config中$ P $下面的条目从显示出来的默认浏览器页面的网站pvents浏览器。

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.

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

但是,这仍然留下了我的第一个疑问。我曾用一个合理的方法来阿贾克斯编辑?

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

我检查与小提琴手的反应,这是IIS被发送默认的错误页500。因此,需要进行配置,返回详细响应的服务器错误。我们可以使用IIS管理器执行此操作。我是继另一种选择是在应用层面来定义它在应用程序的web.config文件中。

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.

不过,我又是一个两难选择是否要使用这种方法或返工我的整个应用程序。出现的问题是,如果真的是有一些模块500内部服务器失败,我的code被在浏览器中暴露的errorMode设置为详细。所以,我需要一些方法来提供本人定制响应当我设置响应错误code至500,如果应用程序的任何其他部分失败(我不是一个设置500状态code),则默认的错误页面应该是发送,而不是通过详细的反应暴露我的code。任何帮助请。

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.

问候, NIrvan。

regards, NIrvan.

推荐答案

新的IIS 7:你必须设置的 Htt的presponse.TrySkipIisCustomErrors 为true,以覆盖IIS的错误页面设置,如果你的web.config的的customErrors设置为ON(或仅限远程你是远程的。)请参见这个答案

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

这篇关于有关疑虑错误在ASP.net MVC的Ajax请求处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆