Umbraco 7 Update Umbraco Routes

本小妞迷上赌 提交于 2019-11-30 05:32:20

this is what I have achieved in my project. Digging inet I found the solution:

  1. 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.

  2. 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 :)

  3. 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 :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!