消息弹出Mvc3与PostSharp OnException处理问题


  1. 当抛出了一些异常
  2. 我正在使用PostSharp作为全局AOP捕获异常并处理它们的框架构建文本弹出窗口的
  3. 我已经将ActionResult扩展到自定义对象在ExecuteResult中实现RenderViewToString方法为messagePopup创建正确的html代码
  4. MessagePopup显示在页面上,但Action继续执行它自己

我该如何阻止它继续自我执行

当它失败时,我会在上在全球范围内捕捉到它

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

在返回我的Message Popup后,它会继续执行原始操作,就好像它没有崩溃一样,当然也会崩溃,因为数据源没有初始化。

我不想用HandleErrorAttribute处理错误,因为它不像PostSharp那样动态。

如何停止原始请求的剩余部分(注意,我正在使用mvc的Telerik网格来显示数据。(

要停止该方法正常进行,请使用args。除非有其他机制使用try/catch,否则该方法应该已经完成了这项操作,但您的方面应该将自己作为最外层的try/catch来应用。你真的需要使用ILSpy查看程序集的最终IL(此时其他反编译器都不会看到postsharp修改(,然后你就能看到发生了什么。如果你有action属性,那么我敢打赌这与它有关,因为postsharp会修改方法,但action属性不会,所以它仍然是流的最外层控制器。请先尝试FlowBehavior。

最新更新