在 MVC 和 Umbraco 项目中使用非 Umbraco 数据? [英] Working with non-Umbraco data in MVC and Umbraco project?

查看:24
本文介绍了在 MVC 和 Umbraco 项目中使用非 Umbraco 数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个安装了 MVC 4 和 Umbraco CMS 的项目.我很抱歉 - 作为新手,我的问题可能很奇怪.

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

我的问题是:如何处理我不想通过 Umbraco 后台管理的类型?相反,它将是简单的数据传入并存储在 SQL Server 中.

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.

我特别想问:

  • 我可以在 MVC 中创建控制器并绕过 Umbraco 吗?
  • 应该继承哪些控制器?它们应该是标准的 MVC 控制器,SurfaceController 还是 RenderMvcController?同样,它不会是 Umbraco 文档类型或数据.
  • 视图会继承自 UmbracoViewPageUmbracoTemplatePage 还是标准的 MVC 视图?
  • 这些视图、控制器和操作的 URL 将如何变化?在 Umbraco 中,URL 取决于内容树,但在非 Umbraco 控制器、视图和操作中如何?
  • 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.

推荐答案

你在这里问了很多不同的问题.

You are asking a lot of different questions here.

在使用 umbraco 进行开发时,Umbraco 将外部数据嵌入您的网站的情况并不少见.如果我们已经告诉您,您可以(几乎)使用您在普通 .Net 项目中使用的任何类型的数据访问.

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.

在提取外部数据(例如)产品时,重要的是不要丢失 umbraco 上下文.您仍然需要渲染面包屑,设置活动菜单的 css 类等等.您的外部数据"可能属于某个节点下方.因此,使用标准 MVC 控制器是一个坏主意.

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.

因为您的视图是剃刀式的,所以您可以将外部数据的每次提取都放入视图中的 @{ ... } 中.如果你不是一个有经验的程序员,这行得通.尽管关于可维护性和 DRY 原则的主题有问题:-)

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 时,您基本上是在为特定的控制器创建控制器文档类型.每次 umbraco 渲染此文档类型的节点时.这个控制器将被调用,你渲染的模型被发送回视图.您可能会猜到,这是我最喜欢提取数据并将其推送到视图的地方之一.另一方面,表面控制器是局部视图的控制器,非常适合处理表单回发.这两个控制器都可以用于您网站的前端,而不是后端.

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.

你可以用你的观点做你想做的.但是,如果您从 UmbracoViewPage 继承您的视图,您仍然可以在视图中使用所有 @Umbraco.Whatever 功能

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

因为您使用 RenderMvcController 劫持"了一个路由,所以您可以相信 umbraco 后端会转到正确的 URL.查询字符串可用于获取您想要的外部数据.

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.

有时,如果我不能使用上面的控制器,我会在 IPublishedContent 上创建一个 extensionMethod.像这样我可以写这样的代码:

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
}

如果您需要公开数据(使用 webApi 包装器),请尝试使用 UmbracoApiController.这个 REST 纯唱.

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

您应该知道 Umbraco 使用 petapoco 作为 ORM.因此,您也可以(并且应该)考虑使用它.您可以毫无问题地重用数据库连接.

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();

这篇关于在 MVC 和 Umbraco 项目中使用非 Umbraco 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆