Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Danny 4 posts 55 karma points
    Apr 27, 2014 @ 18:47
    Danny
    0

    Adding Application Startup code

    Hi 

    I am new to Umbraco but enjoying it however I have a really frustrating problem. I am trying to fire code on the application startup event to register Routes and other things. After building the application and browsing Umbraco CMs everything works fine. Howver when I look at my site it is completely blank and no markup is being rendered in the page source. I am using the ApplicationEventHandler Base class to override the started event but nothing is working. After looking at the event viewer I see the following message:

    Process information: 

        Process ID: 13160 

        Process name: iisexpress.exe 

        Account name: MACKIEVISTA\danny 

     

    Exception information: 

        Exception type: TargetInvocationException 

        Exception message: Exception has been thrown by the target of an invocation.

       at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)

       at System.Activator.CreateInstance(Type type, Boolean nonPublic)

       at System.Activator.CreateInstance(Type type)

       at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)

     

    Value cannot be null.

    Parameter name: umbracoContext

       at Umbraco.Web.Mvc.UmbracoController..ctor(UmbracoContext umbracoContext)

       at Umbraco.Web.Mvc.UmbracoController..ctor()

       at Umbraco.Web.Mvc.RenderMvcController..ctor()

       at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)

     

     

     

    Request information: 

        Request URL: http://localhost:38324/favicon.ico ;

        Request path: /favicon.ico 

        User host address: ::1 

        User:  

        Is authenticated: False 

        Authentication Type:  

        Thread account name: MACKIEVISTA\danny 

     

    Thread information: 

        Thread ID: 10 

        Thread account name: MACKIEVISTA\danny 

        Is impersonating: False 

        Stack trace:    at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)

       at System.Activator.CreateInstance(Type type, Boolean nonPublic)

       at System.Activator.CreateInstance(Type type)

       at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)

    My class file running the Started code (which is in the App_Code folder) is as follows

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Http;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Umbraco.Web;
    using Umbraco.Core;
    using System.Web.Optimization;

    namespace AmaUmbraco.Global
    {
        public class AMAGlobal : ApplicationEventHandler
        {

            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplicationApplicationContext applicationContext)
            {
                base.ApplicationStarted(umbracoApplicationapplicationContext);
                AreaRegistration.RegisterAllAreas();

                WebApiConfig.Register(GlobalConfiguration.Configuration);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
            }
        }

    }
    I am using VS2012 Ultimate, Umbraco 7 and building an MVC 4 website using RAZOR. Please can someone help me ou there. I have wasted hour after hour looking at this and can't figure out why the umbracoContext parameter is null and causing this issue. I have looked at all of the articles I can find on this and tried all manner of different approaches but still the same result.
    regards
  • Danny 4 posts 55 karma points
    Apr 27, 2014 @ 21:58
    Danny
    100

    Hi All

    Looks like this may have been a newby mistake. After closer investigation I thought maybe the reason nothing would render was due to my routing table in some way. So I have taken a look and realised that the default MVC route was still being registered as below

        public class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                routes.MapRoute(
                    name"Default",
                    url"{controller}/{action}/{id}",
                    defaultsnew { controller = "Home"action = "Index"id = UrlParameter.Optional }
                );
            }
        }

     Once I removed my default route and replaced it with my contact controller route everything started to work perfectly.

            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                routes.MapRoute(
                    name"Default",
                    url"ContactUs/{action}/{id}",
                    defaultsnew { controller = "ContactUs"action = "Index"id = UrlParameter.Optional }
                );
            }

    As umbraco has it's own routing, my custom route was interfeering with Umbraco' main controller and causing things to fail. I hope this will help some other newbies to the world of Umbraco.

    Regards

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies