Asp.Net MVC 进阶篇:路由匹配 实现博客路径 和文章路径
2013-08-30 17:57
441 查看
Asp.Net MVC 进阶篇:路由匹配 实现博客路径 和文章路径
我们要实现
通过路由 匹配出 博客地址 和博客文章地址
例如下面的这两个地址
//http://www.cnblogs.com/maijin/
//http://www.cnblogs.com/maijin/archive/2009/01/12/1374473.html
通过路由配置 让控制器能处理 用户的不同提交
第一步 写默认路由规则
第二步 控制器处理
第三步 视图
视频教程 下载
http://pan.baidu.com/share/link?shareid=1571916703&uk=3576826227
源码下载
http://www.bamn.cn/thread-1150-1-1.html#source.rar
我们要实现
通过路由 匹配出 博客地址 和博客文章地址
例如下面的这两个地址
//http://www.cnblogs.com/maijin/
//http://www.cnblogs.com/maijin/archive/2009/01/12/1374473.html
通过路由配置 让控制器能处理 用户的不同提交
第一步 写默认路由规则
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); //路由 其实 说白了 就是 怎么从用户提交的网址 匹配出 那个控制器controller 来处理这个请求 //http://www.cnblogs.com/maijin/ //routes.MapRoute("Blog", "{controller}/{index}/{name}", new { controller = "Blog", action = "Index" }); routes.MapRoute("Blog", "{name}", new { controller = "Blog", action = "Index" }); //http://www.cnblogs.com/wintersun/archive/2009/01/12/1374473.html //routes.MapRoute("Archive", "{name}/{controller}/{year}/{month}/{day}/{id}.html", new {controller = "Archive",action = "Index",year = @"\d4",month = @"\d2",day = @"\d2",id = @"\d+"}); routes.MapRoute("Archive", "{name}/archive/{year}/{month}/{day}/{id}.html", new { controller = "Archive", action = "Index", year = @"\d4", month = @"\d2", day = @"\d2", id = @"\d+" }); routes.MapRoute("DataTime", "p/{datatime}", new { controller = "Archive", action = "DataTimeText" }); //http://localhost:64301/ //http://localhost:64301/home/ //http://localhost:64301/home/index //http://localhost:64301/home/index/12 //controller = null action = null id = routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); }
第二步 控制器处理
public class BlogController : Controller { // // GET: /Blog/ public ActionResult Index(string name) { ViewData["name"] = name; return View(); } }
第三步 视图
@{ ViewBag.Title = "Index"; } <h2>@ViewData["name"].ToString()</h2>
视频教程 下载
http://pan.baidu.com/share/link?shareid=1571916703&uk=3576826227
源码下载
http://www.bamn.cn/thread-1150-1-1.html#source.rar
相关文章推荐
- Asp.Net MVC 进阶篇:路由匹配 实现博客路径 和文章路径
- 在ASP.NET MVC中通过URL路由实现对多语言的支持
- ASP.NET MVC路由匹配检测组件RouteDebug.dll
- ASP.NET MVC路由匹配检测组件RouteDebug.dll
- ASP.NET MVC学习笔记:(一)路由匹配
- asp.net mvc 中"未找到路径“/favicon.ico”的控制器或该控制器未实现 IController。"
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第五章:排序、分页和路由
- ASP.NET MVC 加.mvc后缀,传入的请求不与任何路由匹配解决办法
- 利用ASP.NET MVC和Bootstrap快速搭建个人博客之文章打赏功能(六)
- 在ASP.NET MVC中通过URL路由实现对多语言的支持
- ASP.NET MVC 2博客系列之一:强类型HTML辅助方法 之scottgu的文章
- ASP.NET MVC自定义路由 - 实现IRouteConstraint限制控制器名(转载)
- asp.net MVC实现文章的“上一篇下一篇”
- asp.net mvc 通过修改路由规则来实现页面的URL多参数传递
- ASP.NET没有魔法——ASP.NET MVC 路由的匹配与处理
- ASP.NET MVC路由匹配检测组件RouteDebug.dll
- 在ASP.NET MVC中通过URL路由实现对多语言的支持
- 利用ASP.NET MVC和Bootstrap快速搭建个人博客之文章打赏功能(六)
- ASP.NET MVC路由匹配检测组件RouteDebug.dll
- ASP.NET MVC 路由进阶(之一)