如何阻止它继续执行?
当它失败时,我会在
全局捕捉它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;
}
}
在处理消息内容
之后,正在构建我的ActionResultpublic class 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那样动态。
如何停止原始请求的遗骸? (注意,我正在使用Telerik网格为mvc显示数据。)
答案 0 :(得分:1)
要停止方法继续正常使用args.FlowBehavior.Return。除非有一些使用try / catch的其他机制,否则该方法应该已经执行此操作,但您的方面应该将自身应用为最外层的try / catch。你真的需要使用ILSpy来查看你的程序集的结尾IL(没有其他的反编译器会在这个时间点看到postharp修改)然后你将能够看到发生了什么。如果你有一个动作属性,那么我敢打赌它与它有关,因为postharp将修改方法但动作属性不会因此它仍然是流量的最外层控制器。首先尝试FlowBehavior。