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 > ) 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 = , part = , pager = },
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 = , pager = },
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 = },
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 = },
namespaces: namespaces); map.Add("news/sales", );
map.Add("sales", );
map.Add("news/faq", );
map.Add("faq", );
map.Add("news/company", );
map.Add("company", );
map.Add("video", );
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", );
map.Add("news", );
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 = },
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);
}
}
}

MVC路由配置例的更多相关文章

  1. MVC 伪静态路由、MVC路由配置,实现伪静态。

    前段时间,研究了一下mvc路由配置伪静态,在网上扒了很多最后还是行不通,所以我现在把这些心得整理出来,供大家分享: 1.mvc中默认路由配置是:http://localhost:24409/Home/ ...

  2. MVC路由配置

    目录 URL Routing 的定义方式 示例准备 给片段变量定义默认值 定义静态片段 自定义片段变量 自定义片段变量的定义和取值 将自定义片段变量作为Action方法的参数 指定自定义片段变量为可选 ...

  3. 自定义MVC路由配置

    首先我用MVC4新增一个订单查看的功能 1.创建控制器OrderController namespace MvcApplication3.Controllers { public class Orde ...

  4. 理解MVC路由配置(转)

    在上一篇文章中,我简短的谈了一下MVC的底层运行机制,如果对MVC还不是很了解的朋友,可以作为入门的参照.接下来,我开始介绍关于URL路由的相关知识.URL路由不是MVC独有的,相反它是独立于MVC而 ...

  5. mvc路由配置.html结尾的伪静态

    mvc 标准的写法 通常是(http://localhost:8149/Home/Index) 路由配置如下: 有时候需求 如 http://localhost:8149/Home/Index 改为h ...

  6. 史上最全的ASP.NET MVC路由配置

    MVC将一个Web应用分解为:Model.View和Controller.ASP.NET MVC框架提供了一个可以代替ASP.NETWebForm的基于MVC设计模式的应用. AD:51CTO 网+ ...

  7. ASP.NET MVC路由配置(转载自http://www.cnblogs.com/zeusro/p/RouteConfig.html )

    把apress.pro.asp.net.mvc.4.framework里的CHAPTER 13翻译过来罢了. XD 首先说URL的构造. 其实这个也谈不上构造,只是语法特性吧. 命名参数规范+匿名对象 ...

  8. ASP.NET MVC路由配置

    一.命名参数规范+匿名对象 routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}" ...

  9. 史上最全的ASP.NET MVC路由配置,以后RouteConfig再弄不懂神仙都难救你啦~

    继续延续坑爹标题系列.其实只是把apress.pro.asp.net.mvc.4.framework里的CHAPTER 13翻译过来罢了,当做自己总结吧.内容看看就好,排版就不要吐槽了,反正我知道你也 ...

随机推荐

  1. ubuntu14.04 yuv文件的播放及视频信息的查看

    1.安装ffmpeg sudo add-apt-repository ppa:mc3man/trusty-media sudo apt-get update sudo apt-get install ...

  2. [LeetCode] Largest Number 最大组合数

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  3. [LeetCode] Missing Ranges 缺失区间

    Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...

  4. SQLite3源程序分析之虚拟机

    前言 最早的虚拟机可追溯到IBM的VM/370,到上个世纪90年代,在计算机程序设计语言领域又出现一件革命性的事情——Java语言的出现,它与c++最大的不同在于它必须在Java虚拟机上运行.Java ...

  5. UNC 目录格式检测C#代码

    /// <summary> /// if path is UNC( Universal Naming Convention) path return or return false. // ...

  6. 篇二:MySQL存储过程

    目的:写一个存储过程,往数据库中插入几百条数据,作为识别码给别人用(这里我觉得和验证码的功能相似) BEGIN ); ); ) ; ); ); ; ; while count <= insert ...

  7. iOS 设置不同的字体颜色

    //设置不同字体颜色 -(void)fuwenbenLabel:(UILabel *)labell FontNumber:(UIFont *)font AndRange:(NSRange)range ...

  8. 递推 hdu 2064

    z[n] n个盘子从1到3次数 先想2个的时候  1->2 2->3  1->2 3->2 2->1 2->3 1->2 2->3 显然 要先把上面n- ...

  9. vue+webpack实践

    最近使用了vue来做SPA应用,记一波学习笔记. 使用vue+webpack实现前端模块化. vuejs——轻量.学习成本低.双向绑定.无dom的操作.组件的形式编写 推荐官方文档 vue.js官方文 ...

  10. 记一周cdqz训练

    #include <cstdio> using namespace std; int main(){ puts("转载请注明出处:http://www.cnblogs.com/w ...