- 我有一些麻煩,返回彈出消息在mvc3 時,一些異常被引發。
- 我使用PostSharp作爲全局AOP 框架來捕捉異常並處理它們,構建彈出框的文本 。
- 我已經將ActionResult擴展爲自定義對象,其中的ExecuteResult中的 實現了RenderViewToString方法, 爲messagePopup創建了正確的html代碼。
- MessagePopup顯示在頁面上,但Action繼續執行。
我該如何阻止它繼續執行本身?消息彈出Mvc3與PostSharp的OnException處理問題
當它失敗我抓住它在全球的
namespace Aop
{
/// <summary>
/// Handles Aspect Object Programming in all the projects .
/// The attribute is being injected through Shared AssemblyInfo.cs to all the
/// relevant Assemblies in the project.
/// The code of the class is being added to project in compilation time
/// and by that improves the response time quality
/// </summary>
[Serializable]
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class |
AttributeTargets.Method | AttributeTargets.Constructor,
AllowMultiple = true, Inherited = false)]
[MulticastAttributeUsage(MulticastTargets.Method, AllowMultiple = true,
AllowExternalAssemblies = true)]
public sealed class TraceAttribute : OnMethodBoundaryAspect
{
[Inject]
public IReportBLImpl _reportBL { get; set; }
public TraceAttribute() { }
#region Runtime semantics
/// <summary>
/// Handles all exception in all the project Ness.DoarKamuti exceptions
/// </summary>
/// <param name="eventArgs"></param>
public override void OnException(MethodExecutionEventArgs eventArgs)
{
…
DefActionResult res = DefActionResult.Create("~/Views/Shared/MessagePopup.ascx",_report , DefConstants.MessageDesign.PopUp, "messagePopupBody");
eventArgs.ReturnValue = res;
}
}
比它的建設礦井的ActionResult,處理郵件內容
公共類DefActionResult後:ActionResult的 {
public override void ExecuteResult(ControllerContext context)
{
DefJsonResult model = this.ActionModel;
/* If a view name has been specified, render it */
if (!string.IsNullOrEmpty(model.ViewName))
model.ViewHTML = controller.RenderViewToString(model.ViewName, model.ViewModel);
JsonResult res = new JsonResult() { Data = model, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
res.ExecuteResult(context);
}
}
然後我在建立迴應
public static class MVCExtensions
{
public static string RenderViewToString(this Controller controller, string viewName, object viewData)
{
//Create memory writer
var sb = new StringBuilder();
var memWriter = new StringWriter(sb);
//Create fake http context to render the view
var fakeResponse = new HttpResponse(memWriter);
var fakeContext = new HttpContext(HttpContext.Current.Request, fakeResponse);
var fakeControllerContext = new ControllerContext(
new HttpContextWrapper(fakeContext),
controller.ControllerContext.RouteData,
controller.ControllerContext.Controller);
var oldContext = HttpContext.Current;
HttpContext.Current = fakeContext;
//Use HtmlHelper to render partial view to fake context
var html = new HtmlHelper(new ViewContext(fakeControllerContext,
new FakeView(), controller.ViewData, controller.TempData, memWriter),
new ViewPage());
html.ViewDataContainer.ViewData = controller.ViewData;
html.RenderPartial(viewName, viewData);
//Restore context
//HttpContext.Current = oldContext;
//Flush memory and return output
memWriter.Flush();
return sb.ToString();
}
在返回我的消息彈出窗口之後,它會繼續原來的操作,就好像它沒有粉碎,當然因爲數據源未初始化而崩潰。
我不想用HandleErrorAttribute處理錯誤,因爲它不像PostSharp那樣動態。
如何停止原始請求的遺留物? (一句話,我正在使用Telerik網格來顯示數據。)
嗨,感謝您的快速響應。我已經定義: eventArgs.FlowBehavior = FlowBehavior.Return; ILSpy - 我應該考慮哪些部分?控制器層? Mvc項目? –
流動行爲有效嗎?如果沒有,那麼你需要通過查看ILSpy中的代碼來了解原因。使用ILSpy並從bin文件夾中打開項目的程序集(其中包含您遇到問題的控制器)。一旦打開,找到出現問題的控制器,看看發生了什麼。 –
謝謝!顯然麻煩在於jquery ajax調用不準確,這是額外的呼籲繼續進程.. p.s.ILSpy很好,但要理解IL_150的所有內容都是一個問題; .. –