分享 [ASP.NET]几个常用的MVC设置
2016-11-17 17:55
453 查看
一.默认路由(MVC自带)
复制代码
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{id}", // 带有参数的 URL
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值 (UrlParameter.Optional-可选的意思)
);
}
复制代码
二.不带参数的路由
routes.MapRoute
(
"NoParameter",
"{controller}/{action}/{id}"
);
三.带命名空间的路由
routes.MapRoute(
"AdminControllers", // 路由名称
"{controller}/{id}-{action}", // 带有参数的 URL
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // 参数默认值
new string[] { "Admin.Controllers" }//命名空间
);
四.带约束的路由规则(约束的意思就是用正则这类约束必须符合条件才可以)
routes.MapRoute(
"RuleControllers",
"{controller}/{action}-{Year}-{Month}-{Day}}",
new { controller = "Home", action = "Index", Year = "2010", Month = "04", Day = "21" },
new { Year = @"^\d{4}", Month = @"\d{2}" } //4位数 2位数
);
五.带名称空间,带约束,带默认值的路由规则
复制代码
routes.MapRoute(
"Rule1",
"Admin/{controller}/{action}-{Year}-{Month}-{Day}",
new { controller = "Home", action = "Index", Year = "2010", Month = "04", Day = "21" },
new { Year = @"^\d{4}", Month = @"\d{2}" },
new string[] { "Admin.Controllers" }
);
复制代码
六.捕获所有的路由
routes.MapRoute(
"All", // 路由名称
"{*Vauler}", // 带有参数的 URL
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
);
复制代码
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{id}", // 带有参数的 URL
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值 (UrlParameter.Optional-可选的意思)
);
}
复制代码
二.不带参数的路由
routes.MapRoute
(
"NoParameter",
"{controller}/{action}/{id}"
);
三.带命名空间的路由
routes.MapRoute(
"AdminControllers", // 路由名称
"{controller}/{id}-{action}", // 带有参数的 URL
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // 参数默认值
new string[] { "Admin.Controllers" }//命名空间
);
四.带约束的路由规则(约束的意思就是用正则这类约束必须符合条件才可以)
routes.MapRoute(
"RuleControllers",
"{controller}/{action}-{Year}-{Month}-{Day}}",
new { controller = "Home", action = "Index", Year = "2010", Month = "04", Day = "21" },
new { Year = @"^\d{4}", Month = @"\d{2}" } //4位数 2位数
);
五.带名称空间,带约束,带默认值的路由规则
复制代码
routes.MapRoute(
"Rule1",
"Admin/{controller}/{action}-{Year}-{Month}-{Day}",
new { controller = "Home", action = "Index", Year = "2010", Month = "04", Day = "21" },
new { Year = @"^\d{4}", Month = @"\d{2}" },
new string[] { "Admin.Controllers" }
);
复制代码
六.捕获所有的路由
routes.MapRoute(
"All", // 路由名称
"{*Vauler}", // 带有参数的 URL
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
);
相关文章推荐
- 发现几个常用的asp.net MVC Helper 源码
- 主攻ASP.NET.4.5.1 MVC5.0之重生:政府行政网站常用友情链接跳转javascript[干货分享]
- ASP.NET MVC 3 入门级常用设置、技巧和报错
- ASP.NET MVC 3 入门级常用设置、技巧和报错
- ASP.NET MVC 3 入门级常用设置、技巧和报错
- ASP.NET中常用的几个李天平开源公共类LTP.Common,Maticsoft.DBUtility,LtpPageControl
- [分享]ASP.net一个空间多个网站的设置
- 为ASP.NET MVC开发一些常用插件(三)——Repeater
- ASP.NET MVC 设置 Route 使 URL 中不显示 controller 名
- ASP.NET页面几个常用表单验证
- Asp.net MVC 3实例学习之ExtShop(一)————创建应用并设置开发环境
- Asp.net MVC 3实例学习之ExtShop(一)————创建应用并设置开发环境
- 分享:花了一段时间整理和使用的提高ASP.NET效率的几个方面
- 整理了几个毕业设计的Asp.net代码,分享给大家
- ASP.NET MVC中 HelpHtml常用参数总结
- ASP.NET MVC学习笔记 -- NerdDinner实战1 -- 中文Visual Studio 2008的设置修改以实现复数表单数类名
- [分享]ASP.net一个空间多个网站的设置
- asp.net最常用的三十三种编程代码(转载,同大家分享)
- 自己用的一个ASP.Net MVC分页拿出来分享下
- 转几个关于ASP.NET MVC的讨论(英文)