我正在尝试创建一个 Umbraco 7 MVC 应用程序。在这样做时,我希望能够创建在幕后管理数据的自定义控制器。通过我的研究,我发现使用 SurfaceController 是最成功的。但是,该路由将“/umbraco/surface/”添加到页面。例如,我的测试控制器和视图看起来像“/umbraco/surface/Test”。有没有办法管理这些路线并让它简单地转到“/Test”而不向它添加 Umbraco 路线?任何有关如何在 Umbraco 7 中创建自定义控制器的指导都会有所帮助!
问问题
7432 次
1 回答
12
这是我在项目中取得的成就。挖掘inet我找到了解决方案:
在App_Code文件夹中,我创建了带有路由的文件Startup.cs :
using System.Web.Mvc; using System.Web.Routing; using Umbraco.Core; namespace mebli { public class MyStartupHandler : IApplicationEventHandler { public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { //Create a custom routes // News controller RouteTable.Routes.MapRoute( "", "News", new { controller = "News", action = "Index", id = "0" }); RouteTable.Routes.MapRoute( "", "News/Index", new { controller = "News", action = "Index", id = "0" }); RouteTable.Routes.MapRoute( "", "News/{id}", new { controller = "News", action = "Index", id = UrlParameter.Optional }); } public void OnApplicationInitialized( UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { } public void OnApplicationStarting( UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { } } }
这使您可以拥有所需的路线,在我的例子中site.com/News,site.com/News/Index用于索引,site.com/News/123用于单个新闻。
然后我的NewsController是这样的:
using System.Globalization; using System.Linq; using System.Web; using System.Web.Mvc; using Examine; using Umbraco.Core.Models; using Umbraco.Web; using Umbraco.Web.Models; using Umbraco.Web.Mvc; namespace mebli.Controllers { public class NewsController : PluginController { public NewsController() : this(UmbracoContext.Current) { } public NewsController(UmbracoContext umbracoContext) : base(umbracoContext) { } public ActionResult Index(string id) { var criteria = ExamineManager.Instance.DefaultSearchProvider.CreateSearchCriteria("content"); var filterNews = id == "0" ? criteria.NodeTypeAlias("News") : criteria.NodeTypeAlias("News").And().NodeName(id); var resultNews = Umbraco.TypedSearch(filterNews.Compile()).ToArray(); if (!resultNews.Any()) { throw new HttpException(404, "No product"); } if (id == "0") { criteria = ExamineManager.Instance.DefaultSearchProvider.CreateSearchCriteria("content"); var filterNewsRepository = criteria.NodeTypeAlias("NewsRepository"); var newsRepository = Umbraco.TypedSearch(filterNewsRepository.Compile()); var renderModel = CreateRenderModel(newsRepository.First()); return View("NewsIndex", renderModel); } else { var renderModel = CreateRenderModel(resultNews.First()); return View("News", renderModel); } } private RenderModel CreateRenderModel(IPublishedContent content) { var model = new RenderModel(content, CultureInfo.CurrentUICulture); //add an umbraco data token so the umbraco view engine executes RouteData.DataTokens["umbraco"] = model; return model; } } }
它继承自 Umbraco 的PluginController,不要问为什么 :)
第三,我从控制器调用了两个视图 -用于索引的NewsIndex和用于单个新闻的News 。例如,我的 NewsIndex.cshtml 在这里:
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage @{ Layout = "~/Views/Page.cshtml"; } <div id="main-content" class="body news narrow"> <h2>@Model.Content.GetPropertyValue("header")</h2> <ul> @foreach (IPublishedContent news in Model.Content.Children.OrderBy("date desc")) { <li> <span>@Helpers.FormatDate(news.GetPropertyValue("date"))</span> <div> <a href="@Url.Action("Index", "News", new { id = news.Name })">@Helpers.StripHtml(news.GetPropertyValue("Brief").ToString())</a> </div> </li> } </ul> <div class="clr"></div> </div>
实际上我无法解释这段代码中的每一行,因为不久前我开始学习 ASP.Net MVC 和 Umbraco。但我认为这个想法很明确。它有效:)
于 2014-04-08T06:12:06.510 回答