Copied to clipboard

Flag this post as spam?

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


  • Haansi 51 posts 150 karma points
    Sep 11, 2014 @ 06:24
    Haansi
    0

    Creating a custom Controller and View in Umbraco with MVC ?

    I m new to Umbraco, have installed Umbacro CMS 7 in my MVC application and doing some test / learning work.

    I have created a new controller named Home2, please note I don't have a doc type for Home2. I want to create non Umbraco view/ pages that can work with Umbraco. On running I m getting below error:

    enter image description here

    Below is my code for Controller and view, please guide what I should do to create custom controllers and views that can work with Umbraco ? Do I need to create document type even for non Umbraco types ?

    Controller:

      namespace Web.Controllers
    {
        public class Home2Controller : Umbraco.Web.Mvc.RenderMvcController 
        {
            //
            // GET: /Home/
    
            public override ActionResult Index(RenderModel model)
            {
                //Do some stuff here, then return the base method
                return base.Index(model);
            }
    
        }
    }
    

    View:

    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    Hello welcoem to our page....
    

    Thanks

  • Garret 69 posts 309 karma points
    Sep 11, 2014 @ 10:03
    Garret
    0

    Hi Haansi,

    Did you read http://our.umbraco.org/documentation/Reference/Mvc/custom-controllers? This was helpfull for me!

    Did you create a documenttype Home2?
    The umbraco MVC controller logic work with convention over configuration

    As for navigation/routing. If you create a document with documenttype Home2 as the root document. It will render the document with the default template if you navigate to localhost:xxx/

    Kind regards,

    Garret

  • Haansi 51 posts 150 karma points
    Sep 12, 2014 @ 01:06
    Haansi
    0

    Thanks Garret,

    Is it necessary to create document type ? I mean I don't want to manage this through Umbraco, just want to directly code and mange it. Yet my MVC project has installation of Umbraco CMS on top of it. Kindly guide.

  • alper 8 posts 55 karma points
    Jan 24, 2015 @ 13:45
    alper
    0

    Hi there,

    Check out the video below and following the the tutorials:

    https://www.youtube.com/watch?v=sDQwu_DzYyc

    Cheers,

    AL

     

  • alper 8 posts 55 karma points
    Jan 24, 2015 @ 13:46
    alper
    0

    Hi there,

    Check out the video below and following the the tutorials:

    https://www.youtube.com/watch?v=sDQwu_DzYyc

    Cheers,

    AL

     

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Jan 24, 2015 @ 21:49
    Andy Butland
    3

    This issue you are running into here is that - contrary to a standard ASP.Net MVC application - by default Umbraco is going to handle the routing, and so will not be able to resolve your custom controller.  If you want to have something standalone, separate to Umbraco but within the same application, you'll need to configure the routing for this yourself.

    Something like this - assuming a non-Umbraco controller like this:

        public class MyCustomController : Controller
        {
            public ContentResult Index()
            {
                return Content("hello");
            }
        }
    

    You can set up routing for it by adding a class to your application like this:

        public class RoutingHandler : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                RegisterCustomRoutes();
            }
    
            private static void RegisterCustomRoutes()
            {
                RouteTable.Routes.MapRoute(
                   name: "MyCustomController",
                   url: "MyCustom/{action}/{id}",
                   defaults: new { controller = "MyCustom", action = "Index", id = UrlParameter.Optional });
            }
        }
    

    Hope that helps

    Andy

Please Sign in or register to post replies

Write your reply to:

Draft