I am trying to create an Umbraco 7 MVC application. In doing so, I want to be able to create custom controllers that manage data behind the scenes. Through my research, I found using the SurfaceController was the most successful. However, the route adds "/umbraco/surface/" to the page. For example, my Test Controller and View would look like "/umbraco/surface/Test". Is there a way to manage these routes and have it simply go to "/Test" without adding the Umbraco route to it? Any guidance on how to create custom controllers within Umbraco 7 would be helpful!
this is what I have achieved in my project. Digging inet I found the solution:
In App_Code folder I created file Startup.cs with a routes:
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) { } } }
This allows you to have routes like you want, in my case site.com/News, site.com/News/Index for index, site.com/News/123 for individual news.
Then my NewsController is like this:
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; } } }
It is inherited from Umbraco's PluginController, don't ask why :)
And thirdly I have two views being called from the controller - NewsIndex for the index and News for individual news. For example, my NewsIndex.cshtml is here:
@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>
Actually I can't explain every line in this code because have started learning ASP.Net MVC and Umbraco not long ago. But the idea is clear I think. And it works :)
来源:https://stackoverflow.com/questions/22925202/umbraco-7-update-umbraco-routes