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翻译过来罢了,当做自己总结吧.内容看看就好,排版就不要吐槽了,反正我知道你也 ...
随机推荐
- bootstrap自学总结不间断更新
2.栅格系统 container-fluid 自适应宽度100% container 固定宽度(适应响应式) 屏幕宽度=x x>=1200 1170 992< ...
- $x^2+y^2=c^2$
$x^2+y^2=c^2$ 每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis. 每一 ...
- [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- 【BZOJ 4582】【Usaco2016 Open】Diamond Collector
http://www.lydsy.com/JudgeOnline/problem.php?id=4582 排好序后用两个指针直接\(O(n)\)扫,貌似这个东西学名"two pointers ...
- iOS中获取当前时间,设定时间,并算出差值
NSDate *date = [NSDate date];//获取当前时间 NSTimeZone *zone = [NSTimeZone systemTimeZone];//修改时区 NSIntege ...
- 神经网络与深度学习(3):Backpropagation算法
本文总结自<Neural Networks and Deep Learning>第2章的部分内容. Backpropagation算法 Backpropagation核心解决的问题: ∂C ...
- 自己封装的常用NPOI文件导出源码
示例: 1. 2.示例2 源码下载地址:https://github.com/aa1356889/NPOICode
- 微信开发包注意jar版本:
微信java jar的加密key的大小支持 异常java.security.InvalidKeyException:illegal Key Size的解决方案:在官方网站下载JCE无限制权限策略文件( ...
- Java加载资源文件几种方法
from: http://andyzhu.blog.51cto.com/4386758/775836/ import java.net.URL; import org.springframework. ...
- C#工具代码
反射获取当前请求参数 var prop = System.Type.GetType("System.Web.HttpContext, System.Web, Version=4.0.0.0, ...