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 o开发者_如何转开发f 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 -->
}
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);
}
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);
}
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.
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).
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.
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.
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.
<system.webServer>
<httpErrors errorMode="Detailed"/>
</system.webServer>
But that still leaves out my first doubt. Have I used a reasonable approach to Ajax editing ?
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.
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.
regards, NIrvan.
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
精彩评论