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???
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.
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.
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.
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.
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.
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.
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.
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???
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
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
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
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
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
Hi Devin
And you're running MVC as the template engine?
/Jan
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
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
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
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).
Here is the first method:
Here is the second method:
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.
is working on a reply...