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翻译过来罢了,当做自己总结吧.内容看看就好,排版就不要吐槽了,反正我知道你也 ...
随机推荐
- Spring 设值注入 构造注入 p命名空间注入
注入Bean属性---构造注入配置方案 在Spring配置文件中通过<constructor-arg>元素为构造方法传参 注意: 1.一个<constructor-arg>元素 ...
- spring3 循环依赖
循环依赖就是循环引用,就是两个或多个Bean相互之间的持有对方,比如CircleA引用CircleB,CircleB引用CircleC,CircleC引用CircleA,则它们最终反映为一个环.此处不 ...
- Spring 框架的架包分析、功能作用、优点,及jar架包简介
Spring 框架的架包详解 Spring的作用 Spring的优势 由于刚搭建完一个MVC框架,决定分享一下我搭建过程中学习到的一些东西.我觉得不管你是个初级程序员还是高级程序员抑或 ...
- Lua的string和string库总结
Lua有7种数据类型,分别是nil.boolean.number.string.table.function.userdata.这里我总结一下Lua的string类型和string库,复习一下,以便加 ...
- JavaScript检测对象的类属性
function classof(o) { if(o === null) { return "Null"; } if(o === undefined) { return " ...
- 初探Socket
使用Socket Socket是两台主机之间的一个连接,它可以完成7个操作. 连接远程机器 发送数据 接收数据 关闭连接 绑定端口 监听入站数据 在绑定端口上接受来自远程机器的连接 Java中的Soc ...
- ImportError: cannot import name '_imagingtk'
问题描述 使用tkinter画pillow生成的图片时,在tkinter中抛出此异常. 解决方案 pip install -I --no-cache-dir Pillow 更新pillow 重启解决一 ...
- sql server如何分组编号
我们在生产实践中经常会有这样的需求:分组编号. 如下有一个城市区域表region: 我们需要对上表region按city分组,对region进行排序,得到如下结果: 具体sql如下: select c ...
- 常look的Git命令
常用的Git命令 命令 简要说明 git add 添加至暂存区 git add–interactive 交互式添加 git apply 应用补丁 git am 应用邮件格式补丁 git a ...
- Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
使用android studio 时,编译成功但用build apk时却报错 环境: android studio 1.5, jdk1.7 错误:Error:Execution failed for ...