【问题标题】:How to filter Logging in ASP MVC?如何过滤 ASP MVC 中的日志记录?
【发布时间】:2023-04-05 14:31:01
【问题描述】:

我正在使用 C# 和 SQL Server 2005 开发一个 ASP .Net MVC 3 应用程序。 我想根据登录的用户类型自定义我的界面。 我在关注 this tuto 在微软网站中,当他在过滤器类(ActionLogFilterAttribute)中使用 ActionLog 实例时,我被卡住了。 事实上,这个类是在我没有的“StoreDB.designer.cs”类中声明的,因为我使用实体框架的“代码优先”方法创建了我的项目。 我只有这个类的上下文:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
namespace MvcApplication2.Models
{
    public class GammeContext : DbContext
    {
        public GammeContext()
        {
          Database.SetInitializer(new DropCreateDatabaseIfModelChanges<GammeContext>());
        }

        public DbSet<Account> Accounts { get; set; }
        public DbSet<Ns_AFaire> Ns_AFaires { get; set; }
        public DbSet<Famille> Familles { get; set; }
        public DbSet<Fonction> Fonctions { get; set; }
        public DbSet<Fonction_Poste> Fonction_Postes { get; set; }
        public DbSet<Gamme> Gammes { get; set; }
        public DbSet<Historique> Historiques { get; set; }
        public DbSet<Ligne> Lignes { get; set; }
        public DbSet<Phase> Phases { get; set; }
        public DbSet<Poste> Postes { get; set; }
        public DbSet<Produit> Produits { get; set; }
        public DbSet<Profile_Ga> Profil_Gas { get; set; }
        public DbSet<Sous_Famille> Sous_Familles { get; set; }
        public DbSet<UF> UFs { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<Num_Serie> Num_Series { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
        }
    }
} 

这是我创建的过滤器类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;

namespace MvcApplication2.Filters
{
    public class ActionLogFilterAttribute : ActionFilterAttribute, IActionFilter
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            GammeContext db = new GammeContext();
            ActionLog log = new ActionLog()
            {
                Controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
                Action = filterContext.ActionDescriptor.ActionName,
                IP = filterContext.HttpContext.Request.UserHostAddress,
                DateTime = filterContext.HttpContext.Timestamp
            };
            db.AddToActionLogs(log);
            db.SaveChanges();
            base.OnActionExecuting(filterContext);
        }
    }
}

那么,有什么解决办法吗??

【问题讨论】:

    标签: c# asp.net sql-server asp.net-mvc-3 visual-studio-2010


    【解决方案1】:

    他正在使用的 ActionLog 实例是与他的 ActionLog 表相关的实体。

    您需要做的是添加您自己的 ActionLog 实体 - 仅此而已:)

    【讨论】:

    • 您添加模型和映射器类,并将public DbSet&lt;ActionLog&gt; ActionLogs { get; set; } 添加到您的GammeContext。
    猜你喜欢
    • 1970-01-01
    • 2015-05-21
    • 2020-02-26
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多