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 > ) 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路由配置例的更多相关文章
- MVC 伪静态路由、MVC路由配置,实现伪静态。
前段时间,研究了一下mvc路由配置伪静态,在网上扒了很多最后还是行不通,所以我现在把这些心得整理出来,供大家分享: 1.mvc中默认路由配置是:http://localhost:24409/Home/ ...
- MVC路由配置
目录 URL Routing 的定义方式 示例准备 给片段变量定义默认值 定义静态片段 自定义片段变量 自定义片段变量的定义和取值 将自定义片段变量作为Action方法的参数 指定自定义片段变量为可选 ...
- 自定义MVC路由配置
首先我用MVC4新增一个订单查看的功能 1.创建控制器OrderController namespace MvcApplication3.Controllers { public class Orde ...
- 理解MVC路由配置(转)
在上一篇文章中,我简短的谈了一下MVC的底层运行机制,如果对MVC还不是很了解的朋友,可以作为入门的参照.接下来,我开始介绍关于URL路由的相关知识.URL路由不是MVC独有的,相反它是独立于MVC而 ...
- mvc路由配置.html结尾的伪静态
mvc 标准的写法 通常是(http://localhost:8149/Home/Index) 路由配置如下: 有时候需求 如 http://localhost:8149/Home/Index 改为h ...
- 史上最全的ASP.NET MVC路由配置
MVC将一个Web应用分解为:Model.View和Controller.ASP.NET MVC框架提供了一个可以代替ASP.NETWebForm的基于MVC设计模式的应用. AD:51CTO 网+ ...
- ASP.NET MVC路由配置(转载自http://www.cnblogs.com/zeusro/p/RouteConfig.html )
把apress.pro.asp.net.mvc.4.framework里的CHAPTER 13翻译过来罢了. XD 首先说URL的构造. 其实这个也谈不上构造,只是语法特性吧. 命名参数规范+匿名对象 ...
- ASP.NET MVC路由配置
一.命名参数规范+匿名对象 routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}" ...
- 史上最全的ASP.NET MVC路由配置,以后RouteConfig再弄不懂神仙都难救你啦~
继续延续坑爹标题系列.其实只是把apress.pro.asp.net.mvc.4.framework里的CHAPTER 13翻译过来罢了,当做自己总结吧.内容看看就好,排版就不要吐槽了,反正我知道你也 ...
随机推荐
- nodejs核心模块之http
http模块包含以下5个核心类和方法及属性: 核心类 1,http.Agent 2,http.ClientRequest 3,http.Server 4,http.ServerResponse 5,h ...
- java多线程系类:JUC线程池:06之Callable和Future(转)
概要 本章介绍线程池中的Callable和Future.Callable 和 Future 简介示例和源码分析(基于JDK1.7.0_40) 转载请注明出处:http://www.cnblogs.co ...
- [LeetCode] Strobogrammatic Number III 对称数之三
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【经验之谈】Git使用之TortoiseGit配置VS详解
前言 上一篇<[经验之谈]Git使用之Windows环境下配置>: 安装 配置和使用 后记 关于vs中使用git网上的教程大家也可以找到,我当时配置的时候也是按照网上找的教程一步一步来的, ...
- 【JavaWeb学习】文件的上传和下载
一.文件上传 1.1.概述 实现web开发中的文件上传功能,需完成如下二步操作: 在web页面中添加上传输入项 在servlet中读取上传文件的数据,并保存到本地硬盘中 如何在web页面中添加上传输入 ...
- LTE中的各种ID含义
原文链接:http://www.hropt.com/ask/?q-7128.html ECI (28 Bits) = eNB ID(20 Bits) + Cell ID(8 Bits) 换成16进制就 ...
- APP逆向常识
SO库Linux系统下的动态库文件,就像win系统下的dll文件一样.将APK,改成RAR,在Lib目录下.dex(classes.dex)Dex是Android系统中可以在Dalvik虚拟机上直接运 ...
- 【转】微软教学:三种方法屏蔽Win7/Win8.1升级Win10推送
原文地址:http://www.ithome.com/html/win10/199961.htm 微软在2015年6月就开启了Win10升级推送工作,主要是靠<获取Windows10>工具 ...
- DBoW2库介绍
DBoW2库是University of Zaragoza里的Lopez等人开发的开源软件库. 由于在SLAM回环检测上的优异表现(特别是ORB-SLAM2),DBoW2库受到了广大SLAM爱好者的关 ...