A web-application with complex business logic. but I want to use umbraco as the cms.
I also want to have a traditional Model View Controller set up.
I want to have custom ViewModels attached to strongly typed razor views. I want Controllers to process request and return my custom viewmodels.
So far I have done:
A Web Project with Umbaco7 installed.
Created a StartupHandler : IApplicationEventHandler class that difines routes in the
OnApplicationStarted method
A class that implements IApplicationEventHandle in the OnApplicationStarted method - I am defining my Routes - So far just this one:
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
//Create a custom routes
RouteTable.Routes.MapRoute(
"",
"Menu",
new
{
controller = "Menu",
action = "Index"
});
}
I have matching Controller:
public class MenuController : PluginController
{
MenuService menuService;
public MenuController() : this(UmbracoContext.Current)
{
menuService = new MenuService();
}
public MenuController(UmbracoContext umbracoContext): base(umbracoContext){}
public ActionResult Index()
{
var model = new MainMenuViewModel();
model = menuService.GetMenu();
return View("Menu", model);
}
}
<div>webpage with using the custom model</div>
<div>Test</div>
My custom view model inheriets RenderModel..
public class MainMenuViewModel : RenderModel
{
public List<Category> Categories { get; set; }
public MainMenuViewModel() : this(new UmbracoHelper(UmbracoContext.Current).TypedContent(UmbracoContext.Current.PageId)) {
}
public MainMenuViewModel(IPublishedContent content, CultureInfo culture) : base(content, culture) { }
public MainMenuViewModel(IPublishedContent content) : base(content) { }
}
Now when I go to website.com/menu the controller is hit, which what I want, but when it trys to return the view,
I get an error inside the MainMenuViewModel - On this line
public MainMenuViewModel() : this(new UmbracoHelper(UmbracoContext.Current).TypedContent(UmbracoContext.Current.PageId)) {
pageId is NULL...... any one know why?
Could some one please advice me on my set up, and could someone shed any light on how the custom routes are related to the umbraco content tree?
This approach seems to by pass umbraco completely. Do I need to create a node in umbraco called menu...??
I would recommend reading these blog posts from Shannon Deminick, this should help answer some of your questions and get you up and running, from there you should be able to figure things out.
The blogs make reference to v6.1.6 and above, but this should translate over to the latest v7 builds.
A word of warning though, what you are effectively doing is called route hijacking, and using custom MVC routes like you are doing skips certain parts of the Umbraco Request Pipeline (Hence why your pageId is NULL), the blog posts above provide a solution to overcome this issue, but the solution is by no means perfect. Having used this approach myself I have discovered that there are certain compromises you will have to live with if you choose to go down this route.
Integration with the content tree is not trivial, as I am assuming your content is coming from a separate data source, and is therefore not Umbraco content?
Umbraco Surface Controllers which use a postback will not function correctly on a hijacked route when you use a custom MVC route like above.
Umbraco 7 MVC custom Routes
A web-application with complex business logic. but I want to use umbraco as the cms.
I also want to have a traditional Model View Controller set up.
I want to have custom ViewModels attached to strongly typed razor views. I want Controllers to process request and return my custom viewmodels.
So far I have done:
A Web Project with Umbaco7 installed.
Created a StartupHandler : IApplicationEventHandler class that difines routes in the OnApplicationStarted method
A class that implements IApplicationEventHandle in the OnApplicationStarted method - I am defining my Routes - So far just this one:
I have matching Controller:
I have a view - Menu.cshtml with the first line
My custom view model inheriets RenderModel..
Now when I go to website.com/menu the controller is hit, which what I want, but when it trys to return the view,
I get an error inside the MainMenuViewModel - On this line
pageId is NULL...... any one know why?
Could some one please advice me on my set up, and could someone shed any light on how the custom routes are related to the umbraco content tree? This approach seems to by pass umbraco completely. Do I need to create a node in umbraco called menu...??
Very confused! Help please :-\
and whats the Different between Surface Controller VS Plugin Controller Vs Umbraco Controller. Which one do you inherit from? when? Why? How?
I would recommend reading these blog posts from Shannon Deminick, this should help answer some of your questions and get you up and running, from there you should be able to figure things out.
The blogs make reference to v6.1.6 and above, but this should translate over to the latest v7 builds.
http://shazwazza.com/post/Custom-MVC-routing-in-Umbraco
http://shazwazza.com/post/custom-mvc-routes-within-the-umbraco-pipeline/
A word of warning though, what you are effectively doing is called route hijacking, and using custom MVC routes like you are doing skips certain parts of the Umbraco Request Pipeline (Hence why your pageId is NULL), the blog posts above provide a solution to overcome this issue, but the solution is by no means perfect. Having used this approach myself I have discovered that there are certain compromises you will have to live with if you choose to go down this route.
Hopefully that leads you on the correct path.
is working on a reply...