MVC中使用 事物
?
1,MVC中所有的控制器都 繼承自 Controller??
2,我們要對Controller進行處理? 就要寫一個類來繼承自Controller??? 重寫方法 ? 加入自己的處理程序
View Code using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using CDS.CustomAttribute; using System.Transactions;namespace CDS.Manager {[HandleError]public class CDSController : Controller{#region action發(fā)生異常/// <summary>/// action發(fā)生異常/// </summary>/// <param name="filterContext"></param>protected override void OnException(ExceptionContext filterContext){Session["actionException"] = "";base.OnException(filterContext);}#endregion#region 執(zhí)行action/// <summary>/// 執(zhí)行action/// </summary>protected override void ExecuteCore(){if (IsTransationAction())//當前action使用事務(wù) {using (TransactionScope scope = new TransactionScope()){Session["actionException"] = null;base.ExecuteCore();if (Session["actionException"] == null)//沒有發(fā)生異常才提交數(shù)據(jù)庫 {scope.Complete();}Session["actionException"] = null;}}//end action使用事務(wù)else{base.ExecuteCore();}}#endregion#region 判斷當前請求的action是否使用事務(wù)/// <summary>/// 判斷當前請求的action是否使用事務(wù)/// </summary>/// <returns></returns>public bool IsTransationAction(){bool bTrans = false;try{ControllerContext controllerContext = new ControllerContext(new System.Web.Routing.RequestContext(HttpContext, RouteData), this);string actionName = RouteData.GetRequiredString("action");Type tControl = controllerContext.Controller.GetType();ControllerDescriptor controllerDescriptor = new ReflectedControllerDescriptor(tControl);ActionDescriptor actionDescriptor = controllerDescriptor.FindAction(controllerContext, actionName);object[] attrTrans = actionDescriptor.GetCustomAttributes(typeof(IsTransationAction), false);if (attrTrans != null && attrTrans.Length > 0 && ((CDS.CustomAttribute.IsTransationAction)(attrTrans[0])).IsTransaction){bTrans = true;}}catch{}return bTrans;}#endregion} } View Code using System; using System.Collections.Generic; using System.Linq; using System.Web;namespace CDS.CustomAttribute {[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]public class IsTransationAction : Attribute{private bool b_isTransaction = false;public IsTransationAction(bool isTransaction){this.b_isTransaction = isTransaction;}public bool IsTransaction{get{return (this.b_isTransaction);}set{this.b_isTransaction = value;}}} }?
轉(zhuǎn)載于:https://www.cnblogs.com/DamonTang/archive/2012/04/17/2453395.html
總結(jié)
- 上一篇: 移动医疗APP(解决方案)下载
- 下一篇: uva 10986