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翻译过来罢了,当做自己总结吧.内容看看就好,排版就不要吐槽了,反正我知道你也 ...
随机推荐
- 升级xcode8之后出现报错提示,提示swift版本问题
最近Xcode升级了,出现了各种蛋疼的错误提示,今天遇到个导入框架出现了提示Swift版本的问题,具体如下: "Use Legacy Swift Language Version" ...
- LAMP环境配置 linux+apache+mysql+php
虚拟机安装Linux系统: 新建虚拟机过程中选择Linux,下面选择centos或者是Ubuntu Linux切换图像命令:注意只有装了图像界面才可以切换 查看安装环境的版本: rpm -qa 查看安 ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- [LeetCode] Majority Element II 求众数之二
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- EntityFramework之监听者判断SQL性能指标
前言 当我们利用EF这个ORM框架时,我们可能会利用LINQ或者原生的SQL语句来进行数据操作,此时我们无法确定我们的代码是否会给数据库带来一定的负载,当给数据库带来一定的压力时,由于项目中对数据进行 ...
- javascript 中 Promise的使用
有点经验的js前端都知道 ajax异步函数里面的结果不会立即返回,如果你想在一个异步函数得到某个结果后去执行一个语句怎么做? if ( 异步函数 ) { 语句 } 可能很多人都踩过这样坑,这个时候 ...
- 高通vuforia+Unity3D 制作ar app
很简单就可以用Unity3D做出增强现实的一个小例子 新人第一次写博客,若出现错误望指正^_^ 需要下载de东西: unity3d 5.0 http://unity3d.com/get-unity ...
- Python安装pywinauto时遇到error: The read operation timed out解决方法
Python结合Pywinauto 进行 Windows UI 自动化,安装pywinauto时遇到的一些问题: 解决方法:很明显是链接超时国外网站你懂的V_P_N吧,直接通过报错信息的链接复制到浏览 ...
- css权威指南学习笔记
2016-08-03 1,继承 一般大多数框模型属性都是不能继承的.如:padding .margin .border .background 都不能继承. 继承值,完全没有特殊性.(就是优先级最低) ...
- Python 类(一)
这一篇让先抽象的了解下类与实例 一类的定义 从具体的程序设计观点来看: 类是一种数据结构.我们可以使用类来定义包含数据值和行为特性的对象(类对象). 类是封装逻辑和数据的另一种形式. 面向对象设计观点 ...