3
votes

I m new to Umbraco 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"; }

Index

Hello welcoem to our page....

Thanks

3

3 Answers

7
votes

Many people are confused about routing in umbraco, and this is a common question here on SO. Check out my latest answer: Can I change URL of content?

First you need to register your route when the app is starting. Create a new class which inherit from Umbraco.Core.ApplicationEventHandler. Then overwrite ApplicationStarted to add your rules. Like this:

protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
    //Custom route
    RouteTable.Routes.MapRoute(
    "SomeName",
    "Something/{action}/{id}",
    new
    {
        controller = "MyController",
        action = "Index",
        id = UrlParameter.Optional
    });
}

Then create a controller which inherit from RenderMvcController (not a surface controller. Surface and Render controllers are used for 2 different things): http://our.umbraco.org/documentation/reference/Templating/Mvc/surface-controllers)

public class MyController : Umbraco.Web.Mvc.RenderMvcController
{
    public override ActionResult Index(RenderModel model)
    {
        //Do your db stuff here...
        return PartialView("~/Views/Partials/MyView.cshtml", model);
    }
}
2
votes

If you want to create a custom page thats not handled within Umbraco you need to add it to the umbracoReservedUrls list which is found in your web.config eg

<add key="umbracoReservedUrls" value="~/config/splashes/booting.aspx,~/install/default.aspx,~/config/splashes/noNodes.aspx,~/Home2/Index" />
0
votes

Umbraco takes care of all the routing so your page must exist within the content section of the back-office even if it's not umbraco related. So you first create a document type and then a template for it. Once you've done that, you add the page in the content section.

You can then view that page (go to properties for the page in content, then click link to document). At this point you can add a custom controller if you wish and it should derive from RenderMvcController or SurfaceController (if you do form posts)