如何创建自己的数据注释?或创建ActionFilterAttribute

时间:2019-04-13 12:48:22

标签: c# asp.net-mvc controller data-annotations actionfilterattribute

我是数据注释的新手。我想知道是否有人可以在这里为我提供帮助,我有一个运行良好的日志方法,但是我想将其用作数据注释。我的方法从控制器返回活动日志:

using NLog;

    namespace AccounterBL
    {    
        public class LogServices : Attribute
        {
            private static Logger logger = LogManager.GetCurrentClassLogger();
            public virtual void LogInfo(int pageNumber,String filtering)
            {
                logger.Info("Opening page:" + pageNumber.ToString() +
                    ", Current filtering :" + filtering);
            }
            public void LogError(Exception ex)
            {
                logger.Error("Error:"+ ex.ToString() );
            }

        }

    }

这是我的控制器:

 LogServices LogeSeivce = new LogServices();

        public ActionResult Index(string accountIdFilter, int page=1, Sort sortOrder =Sort.Descending)

        {

            try
            {
                int take = 2;
                ViewBag.CurrentSort = sortOrder;
                ViewBag.NameSortParm = sortOrder==Sort.Descending ? Sort.Assending:Sort.Descending;
                var data = BankDepositHistoryServices.GetAllByPagination(sortOrder, accountIdFilter, take, page);
                var result = data.BankDepositHistoriestDTO;
                ViewBag.AccountIdFilter = accountIdFilter;
                int count = data.Count;
                var resultPagedList = new StaticPagedList<BankDepositHistoryDTO>(result, page, take,count);


                LogeSeivce.LogInfo(page, accountIdFilter);
                return View(resultPagedList);

            }
            catch (Exception ex )
            {
                LogeSeivce.LogError(ex);
                return View("Error", new HandleErrorInfo(ex, "resultPagedList", "Index"));
            }

        }

现在,我想知道是否有可能进行数据注释或ActionFilterAttribute,从而可以像这样检查我的控制器:

[LogeSeivces]       
public ActionResult Index(string accountIdFilter, int page=1, Sort sortOrder =Sort.Descending)

        {

            try
            {
                int take = 2;
                ViewBag.CurrentSort = sortOrder;
                ViewBag.NameSortParm = sortOrder==Sort.Descending ? Sort.Assending:Sort.Descending;
                var data = BankDepositHistoryServices.GetAllByPagination(sortOrder, accountIdFilter, take, page);
                var result = data.BankDepositHistoriestDTO;
                ViewBag.AccountIdFilter = accountIdFilter;
                int count = data.Count;
                var resultPagedList = new StaticPagedList<BankDepositHistoryDTO>(result, page, take,count);


                //LogeSeivce.LogInfo(page, accountIdFilter);
                return View(resultPagedList);

            }
            catch (Exception ex )
            {
              //  LogeSeivce.LogError(ex);
                return View("Error", new HandleErrorInfo(ex, "resultPagedList", "Index"));
            }

        }

我搜索了很多,但找不到 谢谢进阶

0 个答案:

没有答案