PageRenderTime 23ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/Samples/Blog/Bifrost.Samples.Blog.Mvc/Global.asax.cs

#
C# | 99 lines | 77 code | 18 blank | 4 comment | 2 complexity | 981ebbc048c4f7c580ef3b8881b0af4d MD5 | raw file
 1using System.Linq;
 2using System.Reflection;
 3using System.Web.Mvc;
 4using System.Web.Routing;
 5using Bifrost.Configuration;
 6using Bifrost.Execution;
 7using Bifrost.Ninject;
 8using Bifrost.Web.Mvc;
 9using Ninject;
10using Configure = Bifrost.Configuration.Configure;
11using Microsoft.Web.Infrastructure.DynamicModuleHelper;
12using Ninject.Web.Mvc;
13
14[assembly: WebActivator.PreApplicationStartMethod(typeof(Bifrost.Samples.Blog.Mvc.MvcApplication), "InitializeNinject")]
15
16
17namespace Bifrost.Samples.Blog.Mvc
18{
19    public class MvcApplication : BifrostHttpApplication
20    {
21        static readonly Bootstrapper _bootstrapper = new Bootstrapper();
22        protected static IKernel Kernel { get; private set; }
23
24        public static void InitializeNinject()
25        {
26            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
27            DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule));
28            Kernel = CreateKernel();
29            _bootstrapper.Initialize(() => Kernel);
30        }
31
32        /// <summary>
33        /// Creates the kernel that will manage your application.
34        /// </summary>
35        /// <returns>The created kernel.</returns>
36        private static IKernel CreateKernel()
37        {
38            var kernel = new StandardKernel();
39            return kernel;
40        }
41
42
43        protected override IContainer CreateContainer()
44        {
45            var container = new Container(Kernel);
46
47            NinjectInstanceProvider.Kernel = Kernel;
48            global::Ninject.Extensions.Wcf.KernelContainer.Kernel = Kernel;
49
50            return container;
51        }
52
53        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
54        {
55            filters.Add(new HandleErrorAttribute());
56        }
57
58        public static void RegisterRoutes(RouteCollection routes)
59        {
60            routes.IgnoreRoute("default.aspx");
61
62            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
63
64            var featuresNamespace = typeof(MvcApplication).Assembly.GetName().Name + ".Features";
65
66            var query = from t in Assembly.GetExecutingAssembly().GetTypes()
67                        where t.Namespace != null && t.Namespace.StartsWith(featuresNamespace) &&
68                              t.IsSubclassOf(typeof(Controller))
69                        select t.Namespace;
70            var namespaces = query.ToArray();
71            
72            routes.MapRoute(
73                "Default", // Route name
74                "{controller}/{action}/{id}", // URL with parameters
75                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults 
76                namespaces
77            );
78
79            routes.MapRoute(
80                "Home",
81                "Posts/List"
82                );
83        }
84
85        public override void OnConfigure(Configure configure)
86        {
87            configure.ExposeEventService();
88            configure.UsingConfigConfigurationSource();
89        }
90
91        public override void OnStarted()
92        {
93            RelocateViews("Features");
94            AreaRegistration.RegisterAllAreas();
95            RegisterGlobalFilters(GlobalFilters.Filters);
96            RegisterRoutes(RouteTable.Routes);
97        }
98    }
99}