Copied to clipboard

Flag this post as spam?

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


  • glenatron 37 posts 132 karma points
    Dec 05, 2013 @ 16:19
    glenatron
    1

    Custom MVC Controllers not receiving requests

    I am getting started with Umbraco and so far it seems to make sense but I am running into problems getting a custom MVC controller to receive requests.

    The system is set up to use MVC and it works for vanilla content where the View data is stored in the Templates directory on the Umbraco administration system. However when I try to set up a Custom Controller according to the Custom Controllers tutorial it just gives me a 404.

    The parts I have done are:

    • Create custom controller ~/Controllers/HatController.cs extending RenderMvcController with an Index method.
    • Create custom template view ~/Views/umbMaster.cshtml for the basic template content.
    • Create custom view ~/Views/Hat/Index.cshtml to match the controller method name.

    When I fire up my debugger and hit http://localhost:9876/Umbraco/Hat I get the "No umbraco document matches the url '/Umbraco/Hat/'." 404 page and no breakpoints in the controller are hit.

    I assume there is something I need to do to add it to my routing system, but the tutorial gives the impression that Umbraco will sort out those routes automatically. That said some tutorials/posts seem to suggest that one can only do any MVC stuff with SurfaceControllers others seem to say what I am doing is alright and that you only need SurfaceControllers for more interactivity. I haven't quite grasped this corner of the system, so I apologise if this is a very simple or obvious question.

    I'm using version 6.1.6.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Dec 06, 2013 @ 21:08
    Jeavon Leopold
    0

    Hi and welcome to Our!

    Could you please post your code for your controller and views?

    Thanks,

    Jeavon

  • glenatron 37 posts 132 karma points
    Dec 07, 2013 @ 00:16
    glenatron
    0

    I certainly can, but if the Controller is never invoked, I'm not sure how much help it will be- seems like the problem should be somewhere before the controller is hit:

    using umbraco.interfaces;
    using umbraco.NodeFactory;
    using Umbraco.Web.Mvc;
    using Umbraco.Web.Models;
    using MyProject.Models;
    
    namespace MyProject.Controllers
    {
        public class HatController : RenderMvcController
        {
    
    
            [HttpGet]
            public ActionResult Index()
            {
                var newsBase = umbraco.uQuery.GetNodeByUrl("/News");
                var childList = newsBase.ChildrenAsList;
                List<NewsStoryViewModel> stories = new List<NewsStoryViewModel>(childList.Count);
    
                foreach (INode newsStory in childList)
                {
                    Node node = newsStory as Node;
                    if (node != null)
                    {
                        NewsStoryViewModel newsModel = new NewsStoryViewModel(node);
                    }
    
                }
                return CurrentTemplate(stories);
            }
    
        }
    }
    

    And the view:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<List<MyProject.Models.NewsStoryViewModel>>
    @{
        ViewBag.Title = "News List";
        Layout = "~/Views/umbMaster.cshtml";
    }
    
    <h2>News</h2>
    <p>Here is the news displayed through the MVC view.</p>
    

    You'll notice that the view doesn't actually do anything yet- I just want to see some result from the page.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Dec 07, 2013 @ 09:43
    Jeavon Leopold
    0

    Ok, looks ok, this controller should run for a nodes that is of document type Hat, is that the alias of your document type?

  • glenatron 37 posts 132 karma points
    Dec 09, 2013 @ 18:57
    glenatron
    0

    No -it's pulling data from the News document type - can I only create a custom controller that shares a name with an existing document type? Or do I have to do something extra to have custom controller names that aren't tied to document types?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Dec 09, 2013 @ 19:03
    Jeavon Leopold
    0

    Yes, by default custom controller name must match the document type name. For them not to match, you will need to use custom routes, see here for documentation, generally I find it makes more sense if document types do match the controllers but it depends on what you are trying to do.

  • glenatron 37 posts 132 karma points
    Dec 09, 2013 @ 22:52
    glenatron
    0

    Thanks, this was exactly what I needed to know. So if I have a document type with child types- for example I have a ContentFolder Document Type with the idea that I might want to have multiple folders in my content with different names - something like this:

    • HomePage
    • ContentPage
    • ContentFolder ( ~/News )
      • News story 1
      • News Story 2
      • News story 3
    • ContentFolder ( ~/Diary )
      • Diary Entry 1
      • Diary Entry 2
      • Diary Entry 3
    • ContentFolder ( ~/Archive )
      • Archive page 1
      • Archive page 2

    Obviously, my background is much more in bespoke web development so Umbraco is pretty new to me- is having the same Document Type used in different places just not an idiomatic way of working with Umbraco?

    It seems as though I could organise the content design quite easily in that way if I wasn't using MVC for a front end but I may be mistaken.

    Also, looking at the document on custom routes, do you know if that is still current? I notice there is now an App_Start folder containing a RouteConfig.cs file which seems like it might be a better fit for managing routes in the current version?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Dec 09, 2013 @ 23:19
    Jeavon Leopold
    0

    Great! Yes, I would recommend a document type for each type and especially if you need a controller. Have you seen the video tutorials on Umbraco.tv? Also you might be interested in the examples you can find in the Hybrid Mvc Framework such as news (video walk through here)

    You shouldn't have a app_start folder or a routeconfig.cs from Umbraco, maybe it's from a new MVC solution in visual studio (generally it is a good idea to delete all files and references after creating a solution, before adding Umbraco via Nuget)?

  • glenatron 37 posts 132 karma points
    Dec 09, 2013 @ 23:33
    glenatron
    0

    Ah, I bet that is from Visual Studio then, no wonder I was getting confused! Thank you very much, this has been most helpful, I'll take a look at those links you suggested.

Please Sign in or register to post replies

Write your reply to:

Draft