从事 ASP.Net Web 表单和 MVC 混合的项目。
场景:在 Web 窗体应用程序中,页面的一部分正在使用 MVC 部分 View 呈现,如下图所示。
在网络表单中 aspx page ,定义了一个 ID = MVCPartialView1 的 Div 并成功使用 Jquery Ajax 可以将返回的 Partial View 绑定(bind)到 Web Form。
$.ajax(
{
success: function (msg) { $("#MVCPartialView1").html(msg); }
});
对于处理错误场景,使用了以下代码。
[AttributeUsage(AttributeTargets.Class)]
public sealed class ErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
// Execute the normal exception handling routine
base.OnException(filterContext);
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.Result = new ViewResult
{
ViewName = "CustomError.aspx"
};
}
}
}
基本 Controller :
[ErrorAttribute]
public class BaseController : Controller
实际问题是当 MVC 部分 View ( Controller )内部发生任何异常时,CustomError 仅在
DIV MVCPartialView1
内部显示但是将 CustomError 显示为 WebForm.aspx
的完整包含是有意义的但预期的 CustomError 消息是:
最佳答案
您可以使其仅在您的场景中与 js 一起使用。
像这样的一些事情:
$.ajax(
{
success: function (msg)
{
if(msg.indexOf("Error") > -1) //You should check if Error page returned
{
window.location.href = "CustomError.aspx"; //Here if redirect to error of full page
}
else
{
$("#MVCPartialView1").html(msg);
}
}
});
关于asp.net - 自定义错误消息 MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31203731/