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