Working with non-Umbraco data in MVC and Umbraco project?
Asked Answered
T

1

6

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.

Trug answered 12/9, 2014 at 0:7 Comment(0)
E
15

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();
Entertainer answered 12/9, 2014 at 8:34 Comment(9)
Thanks @dampee, I highly appriciate your detail reply, guidance and time. You have said it is a bad idea to use standard controller, does it means it is a bad idea but still I can use ?Trug
Another thing if you please can guide is for non Umbraco types (data which I don't want to manage from back office), do I need to create document type ? How will be its url, say for HomeController with index, will it be domain/Home/Index ?Trug
You could use a "standard" mvc controller, but I can not find one good use case to do so.Entertainer
You do not need to create a document type for external data, however, you probably need a placeholder in your website where you want to nest external data. And for that placeholder (and node), you will need a particular documenttype. Your questions about the URLS is pretty easy, your URLS follow the nodes created in your backoffice. If you have /country/language/home, the homecontroller will be triggered on the home node, but not on the country or language nodes. There is no real /index, because the routing is managed by UmbracoEntertainer
I highly appriciate your indepth knowledge and guiding me here. You said in "RenderMvcController versus SurfaceController", is my understanding correct that for forms (get + post requests) I should use SurfaceControllers and for just Get Actions I should use RenderMvcControllers ?Trug
is there a difference in UmbracoViewPage and UmbracoTemplatePage ? In what situations I should use which one ?Trug
Kind of... Post requests are nice abstracted using surface controllers. While RenderMvcControllers serve to hijack the Umbraco node lookup making it the ideal place to insert your own data. Concerning your other question in the comments: only a small difference. But lets stay on topic, this is a Q&A not a forum. Or feel free to ask another question on StackOverflow. ;-)Entertainer
Thank you so much @Entertainer for the answer provided. In relation to it, is there a model or resource that I can study to figure out how to authenticate members that live on another server (outside of Umbraco)? I have an existing SSO server with member data already on it. I simply want to use Umbraco to authorize these external members into the CMS. Is this possible?Rodman
That is outside the scope of this post. But check out github.com/Shazwazza/UmbracoIdentity/wiki/…Entertainer

© 2022 - 2024 — McMap. All rights reserved.