简体   繁体   English

MVC Ajax.error渲染部分视图

[英]MVC Ajax.error render Partial View

While working with the Kendo UI Grid for ASP.NET MVC I am finding some frustrating behavior that I can't seem to understand or find any information on. 在使用Kendo UI Grid for ASP.NET MVC时,我发现一些令人沮丧的行为,我似乎无法理解或发现任何信息。 I have a grid which makes a kendoGrid.saveChanges() request to the server which has some server side validation. 我有一个网格,该网格向具有一些服务器端验证的服务器发出kendoGrid.saveChanges()请求。 If the server-side validation fails, it returns the following result: 如果服务器端验证失败,则返回以下结果:

Response.StatusCode = HTTP_BAD_CLIENT_REQUEST; // const int 400
return PartialView("Error/BadRequest");

The partial view just has some basic HTML for displaying the error information to the user, and the grid's datasource error callback does the following: 局部视图仅具有一些基本的HTML,用于向用户显示错误信息,并且网格的数据源错误回调执行以下操作:

function errorCallback(e) {
    // Handles explicit Ajax and Kendo DataSource error callbacks
    var content = e.responseText || e.xhr.responseText;
    // Wrapper around a kendo.ui.Window to display content
    Core.Alert.raiseAlert(content);
}

When I run my project in debug mode, or publish the Release version of the project to my local machine the e.xhr.responseText value is populated correctly ie it contains the partial view's HTML message. 当我在调试模式下运行项目或将项目的发行版本发布到本地计算机时,将正确填充e.xhr.responseText值,即它包含部分视图的HTML消息。 However, as soon as I move this to the production server the e.xhr.responseText simply contains the value "Bad Request" which is also the HTTP status code I am currently using. 但是,一旦我将其移至生产服务器, e.xhr.responseText简单地包含值"Bad Request" ,这也是我当前正在使用的HTTP状态代码。 I've tried using other status codes but the result is the same (error name is used as the responseText). 我尝试使用其他状态代码,但是结果是相同的(错误名称用作responseText)。

The reason I find this to be so odd is that I am doing something similar in another project for an internal application at our company and this works perfectly fine. 我觉得这很奇怪的原因是,我正在另一个项目中为我公司的内部应用程序做类似的事情,而且效果很好。 They are running on the same versions and tools of both Kendo and ASP.NET. 它们在Kendo和ASP.NET的相同版本和工具上运行。

Can anyone point me in the direct of tools or documentation, Kendo or AJAX, which explain why the response text is not using my partial view result or how I can map the partial view result I am sending to the xhr.responseText property? 谁能指出我在Kendo或AJAX这样的工具或文档的指导下,这些工具或文档解释了为什么响应文本不使用我的部分视图结果,或者如何将发送到xhr.responseText属性的部分视图结果映射到?

Edit: With trying different error codes, I found that certain status codes such 405 (Not Allowed) resulted in the IIS server error html being returned. 编辑:通过尝试不同的错误代码,我发现某些状态代码(例如405(不允许))导致返回IIS服务器错误html。 So now I'm really stumped, why is it that some status codes simply return a name of the request, while others return templated HTML for that error code when I am specifying the return value and view to return? 所以现在我真的很困惑,为什么当我指定返回值和要返回的视图时,某些状态代码只是返回请求的名称,而另一些状态代码返回该错误代码的模板HTML?

Credit for findings and solutions goes to this post . 对于发现和解决方案的荣誉归功于此职位

After doing some digging, I figured out that the issue came down to IIS overriding the content (and therefore partial view) being sent when I used the error HTTP status codes. 经过一些挖掘后,我发现问题出在IIS覆盖了使用错误HTTP状态代码发送的内容(因此是部分视图)的时候。

The solution is to add an <httpErrors> tag to the system.webServer in the web.config. 解决方案是将<httpErrors>标记添加到web.config中的system.webServer I found that the following was sufficient to get my partial views to reach the client. 我发现以下内容足以使我的部分意见到达客户。

<system.webServer>
    <httpErrors existingResponse="PassThrough" />
</system.webServer>

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

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