Working with data other than Umbraco in MVC and Umbraco projects?

I am working on a project that has MVC 4 and Umbraco CMS installed. I apologize - being a beginner, my question may be strange.

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

In particular, I want to ask:

  • Is it possible to create a controller in MVC and bypass Umbraco?
  • Which controllers should be inherited? Should they be a standard MVC controller, SurfaceController or RenderMvcController ? Again, this will not be the type or data of a Umbraco document.
  • Will views be inherited from UmbracoViewPage , UmbracoTemplatePage or can it 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 what about the controllers, views, and actions without Umbraco?

Thank you so much for your precious time, guidance, exchange and help; I appreciate that.

+6
source share
1 answer

Here you ask a lot of different questions.

When developing with umbraco, Umbraco often embed external data into your website. If we already tell you that you can use (almost) any type of data access that you use in simple .Net projects.

Do not lose your context umbraco

This is important when retrieving external data (e.g.) that does not lose your umbraco context . You still have a cracker for rendering, css classes for the active menu for customization, etc. Your "external data" belongs probably below the node. To do this, it’s nice to use standard MVC controllers.

Dirty razor

Since your views are in razor, you can put every external data retrieval in @ {...} in your opinion. If you are not an experienced programmer, this works. Despite the fact that the topics about the principles of maintenance and DRY are doubtful :-)

RenderMvcController and SurfaceController

When you use the RenderMvcController , you basically create a controller for a specific type of document. Every time umbraco displays a node of this type of document. This controller will be called, and the model you render is sent back to the view. As you might have guessed, this is one of my favorite places to extract data and click on it. A surface controller , on the other hand, is a partial view controller, very well crafted by the postback method. Both of these controllers can be used for the interface of your website, and not for the backend.

Inherit your ideas

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

Your URLs remain the same

Since you are "capturing" the route using the RenderMvcController, you can simply trust the umbraco backend to get to the correct URL. The query can be used to retrieve the external data that you want.

Other controllers or methods

Sometimes, if I cannot use the controller above, I create extentionMethod on an IPublishedContent. For example, I can write code like this:

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

If you need to provide data (using the webApi wrapper), try the UmbracoApiController . This pure REST sang.

Access data in umbraco

You should know that Umbraco uses petapoco as an ORM. Therefore, you can (and should) consider using it. 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(); 
+13
source

Source: https://habr.com/ru/post/975162/


All Articles