C# 如何使用Ninject 3在MVC 5中创建消息传递系统?

C# 如何使用Ninject 3在MVC 5中创建消息传递系统?,c#,asp.net-mvc,C#,Asp.net Mvc,如何使用Ninject 3在MVC 5中实现全局消息传递系统? 我试图实现的目标很简单: 我想添加一条消息,如:“您成功完成了任务”,并将其读入_布局视图。我会使用ViewBag、ViewData或TempData。问题是,为了分离关注点,我在控制器范围外完成了很多处理逻辑(通过依赖项注入)。这意味着ViewBag、ViewData和TempData是不可能的(除非我用控制器扩展注入的类) 所以我尝试创建一个全局静态类来处理这个任务。问题在于,该类独立于应用程序的所有用户的身份验证而显示给他们

如何使用Ninject 3在MVC 5中实现全局消息传递系统? 我试图实现的目标很简单:

我想添加一条消息,如:“您成功完成了任务”,并将其读入_布局视图。我会使用ViewBag、ViewData或TempData。问题是,为了分离关注点,我在控制器范围外完成了很多处理逻辑(通过依赖项注入)。这意味着ViewBag、ViewData和TempData是不可能的(除非我用控制器扩展注入的类)


所以我尝试创建一个全局静态类来处理这个任务。问题在于,该类独立于应用程序的所有用户的身份验证而显示给他们

大部分功劳都来自asp.net论坛和他的帖子

你可以在上面的链接中查看他的帖子。代码基本上是一样的,我只是删减了一些我不需要的功能,最后,我使用了Ninject而不是Autofac(使用的是IoC容器级别)

目标: 当使用Ninject 3作为IoC容器时,您希望使用ASP.NET MVC 5实现全局消息传递系统

在开始之前,我将假设以下情况:

private static void RegisterServices(IKernel kernel)
    {
        //Your original code unmodified. Omitted for convenience 

        //This is the messaging system.
        kernel.BindFilter<NotifierFilterAttribute>(FilterScope.Controller, 0);
    }  
  • 您已安装ASP.NET MVC 3或更高版本
  • 您正在使用Ninject 3
  • 您已经配置了Ninject,并且对它有一点了解
  • 我假设您知道基本的关注点分离,并根据良好的判断划分类和文件
  • 虽然不是强制性的,但在本例中,我使用Bootstrap进行HTML格式化 解决方案:

    第一。在您的解决方案上创建一个新文件,并将其命名为“MessageSystem.cs”。这是将保存我们的消息的类文件

    第二。用以下代码填充它:

    public class MessageSystem
    {
         //This is for bootstrap. This will reference what type of alert we will throw.
        public MessageType Type { get; set; }
        //Where the message you want to say will be held
        public string Message { get; set; }
    
        //Creates the HTML string.
        //This outputs the div in HTML with the current message formatted. 
        public string Generate()
        {
            //Div Tag
            var divTag = new TagBuilder("div");
                divTag.AddCssClass("alert alert-" + Type.ToString());
                divTag.InnerHtml = Message + "<span class=\"glyphicon glyphicon-remove js-close\"></span>";
    
                return divTag.ToString();
    
        }
    }
     //The bootstrap alert types.
    public enum MessageType
    {
        success,
        info,
        warning,
        danger
    }
    
    如上所述,通过MVC调用的操作过滤器不支持自动注入构造函数

    别担心,我们快结束了

    在您的解决方案中,您必须为Ninject定义RegisterService()方法,它应该如下所示:

    private static void RegisterServices(IKernel kernel)
    
    五号在该方法中添加以下内容:

    private static void RegisterServices(IKernel kernel)
        {
            //Your original code unmodified. Omitted for convenience 
    
            //This is the messaging system.
            kernel.BindFilter<NotifierFilterAttribute>(FilterScope.Controller, 0);
        }  
    
    在我的例子中,VisualStudio没有自动解决依赖关系。这就是我在这里展示它的原因

    六,。查看解决方案中放置Ninject绑定的位置

    它们通常采用以下方法:

    private void AddBindings()
    
    在其内部,添加以下代码行:

    kernel.Bind<INotifier>().To<Notifier>().InRequestScope();
    
    Ninject将自动创建通知程序对象,并将其插入调用INotifier的任何位置。我们添加InRequestScope()是因为我们需要在整个应用程序中实例化相同的对象。否则它将无法工作,并且您将在操作筛选器上收到一个空的通知程序对象

    七号。在任何地方使用它

     public class ArticleController : Controller
    {
        readonly INotifier notifier;
    
        public ArticleController(INotifier notifier)
        {
           this.notifier = notifier;
        }
    
    
        // GET: Content
        public ActionResult Index()
        {
            notifier.Success("Yahoo!");
            return View();
    
        }
    
    }

    最后一部分:把你的信息放在要阅读的地方。如果希望它们出现在每个页面上,请在_Layout.cshtml文件中放置以下行:

    @Html.ViewContext.DisplayMessages()
    
    输出的HTML来自MessageSystem类的Generate()方法,以防需要修改

    就这样

    我很抱歉说得这么冗长。我只是想说得尽可能清楚

    原始问题的出处:


    您看过信号机了吗
    kernel.Bind<INotifier>().To<Notifier>().InRequestScope();
    
    using Ninject.Web.Common;
    
     public class ArticleController : Controller
    {
        readonly INotifier notifier;
    
        public ArticleController(INotifier notifier)
        {
           this.notifier = notifier;
        }
    
    
        // GET: Content
        public ActionResult Index()
        {
            notifier.Success("Yahoo!");
            return View();
    
        }
    
    @Html.ViewContext.DisplayMessages()