Copied to clipboard

Flag this post as spam?

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


  • Devin Gleason Lambert 27 posts 89 karma points
    May 12, 2014 @ 16:39
    Devin Gleason Lambert
    0

    Adding a Controller to Umbraco 6.1.1

    Hey all,

     

    I am trying to add a simple controller using Umbraco 6.1.1. I can't seem to get any straight information though because I am getting info based on 4.10+, 7, ect. and Umbraco made changes pertaining to controllers between these versions. I've read a few blogs but haven't figured it out.

     

    Currently my config is two projects, Umbraco.Site and Umbraco.Extensions. Umbraco.Extensions has all my compilable code, which is where I am trying to add the controller.

    I am pretty new to Umbraco so it could be that I am missing out on obvious things but I just jumped into this project so I haven't had much time to become too familiar with Umbraco.

     

    Thanks Again,

    Devin

     

    P.S. how come the freenode irc #umbraco channel is so quiet???

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    May 12, 2014 @ 17:44
    Jan Skovgaard
    0

    Hi Devin and welcome to our :)

    Have you had a look in the documentation section? For instance here http://our.umbraco.org/documentation/Reference/Mvc/surface-controllers - Is that what you're after?

    In regards to the irc channel...I don't know...guess no one from the community is really that active on irc. I did not know that it existed before you mentioned it :D

    Most of the community is very active on Twitter - If you're on twitter make sure to monitor the #umbraco hashtag.

    Hope this helps.

    /Jan

  • Devin Gleason Lambert 27 posts 89 karma points
    May 12, 2014 @ 19:43
    Devin Gleason Lambert
    0

    Jan,

    Thanks for the reply. I have looked at the documentations sections including surface-controllers. I don't think what I am looking for is a surface controller because it seems like surface controllers are only used for partial views, correct? What I want at the moment, is to write a simple controller to know my code is working, with a method that returns a string "hello world", then navigate to that page.

    Devin

  • Devin Gleason Lambert 27 posts 89 karma points
    May 12, 2014 @ 21:43
    Devin Gleason Lambert
    0

    To add some detail, I have just copied the first sample code from this example( http://our.umbraco.org/documentation/reference/Templating/Mvc/custom-controllers ), renamed the controller BlahController, added a document type name Blah, added a method that returned a string and tried to navigate to it and no luck.

    Devin

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    May 12, 2014 @ 22:32
    Jan Skovgaard
    0

    Hi Devin

    Perhaps this post can help you understand how it works in Umbraco when using MVC? :) http://umbraco.com/follow-us/blog-archive/2013/7/14/moving-from-webforms-to-mvc.aspx

    Perhaps you can also get some inspiration on how to create a custom controller based on this example? http://our.umbraco.org/documentation/Reference/Mvc/custom-controllers

    Also make sure to read Warren's blogpost about surfacecontrollers http://creativewebspecialist.co.uk/2013/07/22/umbraco-mvc-what-on-earth-is-a-surface-controller/

    Btw...have you changed the template mode to run MVC rather than webforms in the /config/umbracoSettings.config file?

    Hope this helps.

    Cheers, Jan

  • Devin Gleason Lambert 27 posts 89 karma points
    May 13, 2014 @ 14:14
    Devin Gleason Lambert
    0

    I've went as far as this tutorial ( http://shazwazza.com/post/Custom-MVC-routing-in-Umbraco ), added the class that implements ApplicationEventHandler, changed my controller to implement PluginController, UmbracoController, still no luck.

     

    Keep in mind

    Devin

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    May 13, 2014 @ 15:05
    Jan Skovgaard
    0

    Hi Devin

    And you're running MVC as the template engine?

    /Jan

  • Devin Gleason Lambert 27 posts 89 karma points
    May 13, 2014 @ 15:18
    Devin Gleason Lambert
    0

    Jan,

     

    Sorry, I was supposed to post that last post yesterday, some reason it never went through till today. Yes I checked an my defaultRenderingEngine is set to MVC.

     

    Devin

  • Devin Gleason Lambert 27 posts 89 karma points
    May 13, 2014 @ 15:27
    Devin Gleason Lambert
    0

    Jan,

     

    Sorry, I was supposed to post that last post yesterday, some reason it never went through till today. Yes I checked an my defaultRenderingEngine is set to MVC.

     

    Devin

  • Devin Gleason Lambert 27 posts 89 karma points
    May 13, 2014 @ 22:05
    Devin Gleason Lambert
    1

    I figured it out, the problem I was having was the separation of the two projects, Umbraco.Site and Umbraco.Extentions. Since I do not compile Umbraco.Site, all my cs code goes it Extension.This was my problem, in order for it to work, my class where I inherit ApplicationEventHandler class (http://shazwazza.com/post/Custom-MVC-routing-in-Umbraco) had to go in Extension\App_Code.

    Also as a note to those of you who follow the shazwazza tutorial, it doesn't explicitly say or I was unable to get it working exactly as they did, but I needed to override the method  ApplicationStarted() instead of OnApplicationStarted, as well their was no mention of overriding eitherway. This left me with my routing logic never being executed. Very frustrating.

     

    Devin

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    May 13, 2014 @ 22:54
    Nicholas Westby
    0

    Hi Devin,

    Routing has changed over the various versions of Umbraco. For the version you are using, I believe there are a couple ways, neither of which should require that you put your controllers in App_Code. For both techniques, you just add a project to your solution and reference it from your main website. You must be sure to rebuild that project before you run, as I think Visual Studio will sometimes run the website without compiling the other project (rebuilds also ensure the web website restarts, which will ensure the ApplicationStarting method gets called).

    • For the first method, just create a controller named after your document type, and an action method called Index (or, you can create an action method named after your template, which to be clear is a different thing than your document type). Note that you'd inherit the controller from RenderMvcController rather than Controller and you'd return from the action method by calling CurrentTemplate rather than by calling View. Here is an example I have with my ShoppingCart document type and my ShoppingCart template (see below... the new markup editor is acting weird and I'm not sure I can place code in a bulleted list).
    • Alternatively, you can do what it seems you did and use the ApplicationStarting event to map a custom route to a controller/action. That controller can be a normal Controller, in which case you'd call View like normal to return the view (though if you don't use a RenderModel you'll then have to be careful about what type of view you return).

    Here is the first method:

    // Method 1.
    namespace MyNamespace.Extensions.Controllers
    {
        using System.Web.Mvc;
        using Umbraco.Web.Models;
        using Umbraco.Web.Mvc;
        public class ShoppingCartController : RenderMvcController
        {
            [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
            public ActionResult ShoppingCart(RenderModel model) {
                return CurrentTemplate(model);
            }
        }
    }
    

    Here is the second method:

    // Method 2.
    namespace MyNamespace.Extensions.EventHandlers
    {
        using System.Web.Routing;
        using Umbraco.Core;
        public class MyCustomHandlers : ApplicationEventHandler
        {
            protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication,
                ApplicationContext applicationContext)
            {
                var routes = RouteTable.Routes;
                routes.MapRoute(
                    name: "Some Route",
                    url: "some-url",
                    defaults: new { controller = "Whatevs", action = "Index" },
                    constraints: new { controller = "Whatevs", action = "Index" }
                );
                base.ApplicationStarting(umbracoApplication, applicationContext);
            }
        }
    }
    

    By the way, it can be problematic to include "Umbraco" in your custom namespace. I've done that in the past and when I wanted to use the real Umbraco namespaces I had to fully qualify the namespace using the global namespace alias.

Please Sign in or register to post replies

Write your reply to:

Draft