Umbraco 7更新Umbraco路线

flo*_*ear 2 asp.net-mvc umbraco umbraco7

我正在尝试创建一个Umbraco 7 MVC应用程序.这样做,我希望能够创建管理幕后数据的自定义控制器.通过我的研究,我发现使用SurfaceController是最成功的.但是,该路由将"/ umbraco/surface /"添加到页面.例如,我的Test Controller和View看起来像"/ umbraco/surface/Test".有没有办法管理这些路线,让它只是去"/测试"而不添加Umbraco路线?有关如何在Umbraco 7中创建自定义控制器的任何指导都会有所帮助!

The*_*Yur 12

这是我在项目中取得的成就.挖掘inet我找到了解决方案:

  1. 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)
            {
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    这可以让你有路线像你想,在我的情况site.com/News,site.com/News/Index索引,site.com/News/123个人消息.

  2. 然后我的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;
            }
    
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    它继承自Umbraco的PluginController,不要问为什么:)

  3. 第三点我有被称为从控制器两种观点- 新闻索引的索引和新闻个体新闻.例如,我的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>
    
    Run Code Online (Sandbox Code Playgroud)

实际上我无法解释这段代码中的每一行,因为不久前已经开始学习ASP.Net MVC和Umbraco.但我认为这个想法很明确.它的工作原理:)