Fork me on GitHub
.net求学者

MVC路由配置例

public static void RegisterRoutes(RouteCollection routes)
        {
            string suffix = string.Empty;
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            var temp = new System.Text.StringBuilder();
            var methods = typeof(Web.Controllers.HomeController).GetMethods().Where(m => m.ReturnType == typeof(ActionResult) && m.IsPublic == true).ToList();
            temp.Append("(");
            var list = new List<string>();
            foreach (var item in methods)
            {
                if (string.IsNullOrEmpty(item.Name)) continue;
                if (list.Contains(item.Name)) continue;
                if (list.Count > 0) temp.Append("|");
                temp.Append(item.Name);
                list.Add(item.Name);
            }
            temp.Append(")");
            routes.MapRoute(
                name: "HomePage",
                url: "{action}",
                defaults: new { controller = "Home", action = "Index" },
                constraints: new { action = temp.ToString() },
                namespaces: new string[] { "Web.Controllers" }
            );
            #region Artilce

            routes.MapRoute("ArticleListDef", "ArticleList/{id}",
                new { controller = "Article", action = "List",id=UrlParameter.Optional },
                new { id = @"^\d+$" },
                new string[] { "Web.Controllers" });

            routes.MapRoute("ArticleList", "ArticleList/{id}-{pager}",
                new { controller = "Article", action = "List" },
                new { id = @"^\d+$", pager = @"^\d+$" },
                new string[] { "Web.Controllers" });

            #endregion 

            #region Brand
            routes.MapRoute("Brand_List1", "Brand/ArticleList-{id}-{part}-{pager}",
                new { controller = "Brand", action = "ArticleList", id = 0, part = 0, pager = 1 },
                new string[] { "Web.Controllers" });

            routes.MapRoute("Brand_List2", "Brand/{action}-{id}",
                new { controller = "Brand", action = "About" },
                new { action = "(About|Join)" },
                new string[] { "Web.Controllers" });
            routes.MapRoute("Brand_List3", "Brand/{action}-{id}-{pager}",
                new { controller = "Brand", action = "", id = 0, pager = 1 },
                new { action = "(ProductList|Stores|Honor|Comment)" },
                new string[] { "Web.Controllers" });

            #endregion

            #region Comment
            routes.MapRoute("Comment", "Comment-{module}-{key}-{pager}",
                new { controller = "Comment", action = "list", pager = 1 },
                new string[] { "Web.Controllers" });

            #endregion
            routes.MapRoute("ControllerList", "{controller}List/{pager}",
                new { action = "List",pager=UrlParameter.Optional },
                new string[] { "Web.Controllers" });





            routes.MapRoute("ControllerDetail", "{controller}/{id}",
                new { controller = "Article", action = "Detail" },
                new { id = @"^\d+$" },
                new string[] { "Web.Controllers" });

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new string[] { "Web.Controllers" }
            );
        }

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Web.Common;
using Web.Models;
namespace Web
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            var namespaces = new string[] { "Web.Controllers" };
            var part = SiteHelper.Parts;
            
            routes.MapRoute(
                 name: "Error404",
                 url: "Error404",
                 defaults: new { controller = "Error", action = "Error404" },
                 namespaces: namespaces);

            //单例文章 根据 Word访问页面
            foreach (var item in part.Where(p => p.Module == (int)Module.Single && p.Word != string.Empty))
            {
                //单例文章
                routes.MapRoute(
                    name: string.Format("Single-{0}", item.ID),
                    url: item.Word+".html",
                    defaults: new { controller = "Article", action = "Single", part = item.ID },
                    namespaces: namespaces);
            }
            //文章列表页 根据 Word创建目录
            foreach (var item in part.Where(p => (p.Module == (int)Module.Article || p.Module == (int)Module.Topic) && p.Word != string.Empty))
            {
                //栏目文章
                routes.MapRoute(
                    name: string.Format("ArticleDetail-{0}", item.ID),
                    url: item.Word+"/{part}-{id}.html",
                    defaults: new { controller = "Article", action = "Detail", part = item.ID },
                    constraints: new { id = @"(\d+)" },
                    namespaces: namespaces);

                if (item.Module == (int)Module.Topic)
                {
                    routes.MapRoute(
                    name: string.Format("ArticleTopic-{0}", item.ID),
                    url: item.Word + "/{name}.html",
                    defaults: new { controller = "Article", action = "Topic", part = item.ID },
                    constraints: new { name = "([a-z0-9A-Z]+)" },
                    namespaces: namespaces);
                }
                //栏目列表
                routes.MapRoute(
                    name: string.Format("ArticleList-{0}", item.ID),
                    url: item.Word,
                    defaults: new { controller = "Article", action = "Index", part = item.ID },
                    namespaces: namespaces);
            }
            routes.MapRoute(
                name: "Search",
                url: "Search",
                defaults: new { controller = "Article", action = "Search" },
                namespaces: namespaces);
            //作品内容
            routes.MapRoute(
                name: "CaseDetail",
                url: "Case/{id}.html",
                defaults: new { controller = "Case", action = "Detail" },
                constraints: new { id = @"(\d+)" },
                namespaces: namespaces);
            //作品列表
            routes.MapRoute(
                name: "CaseList",
                url: "Case/List-{style}-{type}-{cost}-{a}",
                defaults: new { controller = "Case", action = "Index" },
                namespaces: namespaces);
            //默认详情
            routes.MapRoute(
               name: "ArticleDetail",
               url: "Article/{id}.html",
               defaults: new { controller = "Article", action = "Detail" },
               constraints: new { id = @"(\d+)" },
               namespaces: namespaces);
            routes.MapRoute(
               name: "ArticleSingle",
               url: "Single/{part}.html",
               defaults: new { controller = "Article", action = "Single" },
               constraints: new { part = @"(\d+)" },
               namespaces: namespaces);

            #region UrlRedirect
            Dictionary<string, int> map = new Dictionary<string, int>();
            routes.MapRoute(
                    name: "UrlArticleList_10s",
                    url: "sales",
                    defaults: new { controller = "UrlRedirect", action = "ArticleList", id = 10 },
                    namespaces: namespaces);

            map.Add("news/sales", 10);
            map.Add("sales", 10);
            map.Add("news/faq", 9);
            map.Add("faq", 9);
            map.Add("news/company", 6);
            map.Add("company", 6);
            map.Add("video", 24);
            foreach (var item in map)
            {
                routes.MapRoute(
                    name: "UrlArticleList_" + item.Key,
                    url: item.Key,
                    defaults: new { controller = "UrlRedirect", action = "ArticleList", id = item.Value },
                    namespaces: namespaces);
            }
            map.Add("guide", 7);
            map.Add("news", 4);
            foreach (var item in map)
            {
                routes.MapRoute(
                    name: "UrlArticle_" + item.Key,
                    url: item.Key + "/{id}.html",
                    defaults: new { controller = "UrlRedirect", action = "Article" },
                    namespaces: namespaces);
            }

            routes.MapRoute(
                    name: "UrlTuangouDetail",
                    url: "tuangou/{id}.html",
                    defaults: new { controller = "UrlRedirect", action = "Team" },
                    namespaces: namespaces);
            routes.MapRoute(
                    name: "UrlTuangou",
                    url: "tuangou",
                    defaults: new { controller = "UrlRedirect", action = "Team", id = 0 },
                    namespaces: namespaces);

            routes.MapRoute(
                    name: "UrlPhp",
                    url: "index.php",
                    defaults: new { controller = "UrlRedirect", action = "PHP" },
                    namespaces: namespaces);

            routes.MapRoute(
                name: "UrlAbout",
                url: "About/{id}.html",
                defaults: new { controller = "UrlRedirect", action = "About" },
                namespaces: namespaces);
            #endregion

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: namespaces);
        }
    }
}

 

posted @ 2015-03-27 18:25  hy31337  阅读(260)  评论(0编辑  收藏  举报
.net求学者