6
votes

I am working on a project that has MVC 4 and the Umbraco CMS installed. I apologise - being newbie, my question may be weird.

My question is: how do I work with types which I don't want to manage through Umbraco back office?Rather, it will be simple data coming and being stored in SQL Server.

Specifically I want to ask:

  • Can I create a controller in MVC and bypass Umbraco?
  • What controllers should be inherited from? Should they be standard MVC Controller, SurfaceController or RenderMvcController? Again, it will not be an Umbraco document type or data.
  • Will views be inherited from UmbracoViewPage, UmbracoTemplatePage or it can be a standard MVC view?
  • How will the URL of these views, controllers and actions change? In Umbraco, the URL depends on the content tree, but how it will be with non Umbraco controllers, views and actions?

Thank you so much for your precious time, guidance, sharing and help; I highly appreciate it.

1

1 Answers

15
votes

You are asking a lot of different questions here.

When developing with umbraco Umbraco it's not uncommon to embed external data into your website. If we already tell you that you can use (almost) any type of dataaccess you use in plain .Net projects.

Don't loose your umbraco context

It's important when pulling in external data (e.g.) products, that you don't loose your umbraco context. You still have a breadcrumb to render, css classes for active menu's to set and so on. Your "external data" belongs probably below a node. Therefor it's a bad idea to use Standard MVC controllers.

Dirty razor

Because your views are in razor, you COULD put every extraction of external data into @{ ... } in your view. If you are not an experienced programmer, this works. Although topics about maintainablility and DRY principles are questionable :-)

RenderMvcController versus SurfaceController

When you use a RenderMvcController you basically are are creating a Controller for a specific document type. Each time umbraco is rendering a node of this document type. This controller will be invoked and the model you render is send back to the View. As you might guess, this is one of my favorite places to extract data and push it to the view. A surface controller on the other hand is a controller for a partial view, extremely well in handeling form postbacks. Both of these controllers can be used for the front-end of your website, not for the backend.

Inherit your views

You can do with your views what you want. But if you inherit your view from UmbracoViewPage you still have all the @Umbraco.Whatever power available in your views

Your URLS stay the same

Because you "hijack" a route using the RenderMvcController, you can just trust the umbraco backend to go to the right URL. The querystring can be used to get external data you want.

Other controllers or methods

Sometimes, if I can't use the controller above, I create an extentionMethod on the IPublishedContent. Like that I can write code like this:

foreach (var myObj in Model.Content.GetMyExternalData()) {
   // do stuff
}

Of if you need to expose data (using a webApi wrapper), try the UmbracoApiController. This REST pure sang.

Data Access in umbraco

You should know that Umbraco uses petapoco as ORM. Therefor you can (and should) consider to use it too. You can reuse the database connection without any problems.

var query = new Sql().Select("*").From("myCustomTable").Where<MyModel>(x => x.Id == id);
return DatabaseContext.Database.Fetch<MyModel>(query).FirstOrDefault();