簡體   English   中英

如何添加根級路由(不在區域中)

[英]How do I add root-level routes (not in an area)

在此處完成對Orchard的菜鳥操作(以及CMS的常規操作),所以請糾正我可能有的任何誤解。 我有一個現有站點,正在嘗試將其改編為Orchard模塊。 目標基本上是與以前相同的網站,但所有者可以通過Orchard GUI添加自己的博客文章頁面。

該站點即模塊,我稱為SiteModule ,需要在應用程序的根級別具有路由,以便使其看起來像“我的站點+ CMS”,而不是“ CMS +我的站點” 。

如何在SiteModule構建路由,使它們位於應用程序的根目錄中,而不是默認位於專用的/SiteModule區域中?

您可能會猜到它是默認設置,以防止模塊互相踩到腳趾,但是您可以輕松添加自己的路線。 此代碼假定模塊中有兩個控制器,分別稱為Home和Default。 在您的SiteModule項目的路由中創建一個Routes.cs文件。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Orchard.Mvc.Routes;

namespace SiteModule {
    public class Routes : IRouteProvider {
        public void GetRoutes(ICollection<RouteDescriptor> routes) {
            foreach (var routeDescriptor in GetRoutes())
                routes.Add(routeDescriptor);
        }

        public IEnumerable<RouteDescriptor> GetRoutes() {
            return new[] {
                new RouteDescriptor {
                    Priority = 5,
                    Route = new Route(
                        "Home/{action}",
                        new RouteValueDictionary {
                            {"area", "SiteModule"},
                            {"controller", "Home"},
                            {"action", "Index"}
                        },
                        new RouteValueDictionary(),
                        new RouteValueDictionary {
                            {"area", "SiteModule"}
                        },
                        new MvcRouteHandler())
                },
                new RouteDescriptor {
                    Priority = 5,
                    Route = new Route(
                        "Default/{action}",
                        new RouteValueDictionary {
                            {"area", "SiteModule"},
                            {"controller", "Home"},
                            {"action", "Gogo"}
                        },
                        new RouteValueDictionary(),
                        new RouteValueDictionary {
                            {"area", "SiteModule"}
                        },
                        new MvcRouteHandler())
                }
            };
        }
    }
}

我猜只是要注意,您並沒有覆蓋Orchard的默認路由:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM