Url地址重写与优化的作用主要表现如下
⒈缩短Url,隐藏实际路径提高安全性。
⒉易于用户记忆和键入。
⒊易于被搜索引擎收录。
以下是我在MVC3中准备重写的一些常见的Url
http://www.xxxx.com/pub/users //用户广场
http://www.xxxx.com/zhangsan //用户空间
http://www.xxxx.com/zhangsan/photos //用户所有照片
http://www.xxxx.com/zhangsan/photos?pid=12334324 //用户指定照片
http://www.xxxx.com/zhangsan/photos/date-2012-12 //用户指定时间拍摄的照片
http://www.xxxx.com/zhangsan/photos/date-2012-12?pid=122343243 //用户指定时间拍摄的照片 从指定照片开始
http://www.xxxx.com/zhangsan/photos/tag-美女 //用户拍摄的标签为美女的照片
http://www.xxxx.com/zhangsan //用户空间
http://www.xxxx.com/zhangsan/photos //用户所有照片
http://www.xxxx.com/zhangsan/photos?pid=12334324 //用户指定照片
http://www.xxxx.com/zhangsan/photos/date-2012-12 //用户指定时间拍摄的照片
http://www.xxxx.com/zhangsan/photos/date-2012-12?pid=122343243 //用户指定时间拍摄的照片 从指定照片开始
http://www.xxxx.com/zhangsan/photos/tag-美女 //用户拍摄的标签为美女的照片
我只要在Global.asax.cs文件中做如下配置即可达到重写目的
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Pub-User",
"pub/{action}",
new { controller = "Pub", action = "Index" }
);
routes.MapRoute(
"User-Space",
"{userid}",
new { controller = "User", action = "Index"}
);
routes.MapRoute(
"User-Photos",
"{userid}/Photos",
new { controller = "Space", action = "PhotoViews" ,pid = "[0-9]{6,}"}
);
routes.MapRoute(
"User-Photos-date",
"{userid}/Photos/date-{year}-{month}",
new { controller = "Space", action = "PhotoViewMonths", year = "[0-9]{4}", month = "[0-9]{1,2}" }
);
routes.MapRoute(
"User-Photos-tag",
"{userid}/Photos/tag-{tag}",
new { controller = "Space", action = "PhotoViewTags" }
);
routes.MapRoute(
"Default", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}