一、认识Global.asax.cs
- public class MvcApplication : System.Web.HttpApplication
- {
- protected void Application_Start()
- {
- //注册 ASP.NET MVC 应用程序中的所有区域
- AreaRegistration.RegisterAllAreas();
- //注册 全局的Filters
- FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
- //注册 路由规则
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- //注册 打包绑定(js,css等)
- BundleConfig.RegisterBundles(BundleTable.Bundles);
- }
- }
- public class RouteConfig
- {
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
- );
- }
- }
三、ASP.NET MVC默认的命名约定
1、Controller命名约定
2、View命名约定
四、ASP.NET MVC的URL规则说明
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
- );
URL
|
URL段 |
http://mysite.com/Admin/Index
|
controller = Admin
action = Index
|
http://mysite.com/Index/Admin
|
controller = Index
action = Admin
|
http://mysite.com/Apples/Oranges
|
controller = Apples
action = Oranges
|
http://mysite.com/Admin
|
无匹配-段的数量不够
|
http://mysite.com/Admin/Index/Soccer
|
无匹配-段的数量超了 |
五、mvc创建一个简单的Route规则
- public static void RegisterRoutes(RouteCollection routes) {
- routes.MapRoute("MyRoute", "{controller}/{action}");
- }
- public static void RegisterRoutes(RouteCollection routes) {
- Route myRoute = new Route("{controller}/{action}", new MvcRouteHandler());
- routes.Add("MyRoute", myRoute);
- }
六、mvc路由的默认值的设定
- public static void RegisterRoutes(RouteCollection routes) {
- routes.MapRoute("MyRoute", "{controller}/{action}", new { action = "Index" });
- }
- public static void RegisterRoutes(RouteCollection routes) {
- routes.MapRoute("MyRoute", "{controller}/{action}",
- new { controller = "Home", action = "Index" });
- }
Url段的数量
|
实例
|
Route映射
|
0
|
mydomain.com
|
controller = Home
action = Index
|
1
|
mydomain.com/Customer
|
controller = Customer
action = Index
|
2
|
mydomain.com/Customer/List
|
controller = Customer
action = List
|
3
|
mydomain.com/Customer/List/All
|
无匹配—Url段过多
|
七、mvc使用静态URL段
- public static void RegisterRoutes(RouteCollection routes) {
- routes.MapRoute("MyRoute", "{controller}/{action}",
- new { controller = "Home", action = "Index" });
- routes.MapRoute("", "Public/{controller}/{action}",
- new { controller = "Home", action = "Index" });
- }
- public static void RegisterRoutes(RouteCollection routes) {
- routes.MapRoute("", "X{controller}/{action}");
- routes.MapRoute("MyRoute", "{controller}/{action}",
- new { controller = "Home", action = "Index" });
- routes.MapRoute("", "Public/{controller}/{action}",
- new { controller = "Home", action = "Index" });
- }
八、mvc的路由中自定义参数变量
- public static void RegisterRoutes(RouteCollection routes) {
- routes.MapRoute("MyRoute", "{controller}/{action}/{id}",
- new { controller = "Home", action = "Index", id = "1" });
- }
- public ViewResult CustomVariable() {
- ViewBag.CustomVariable = RouteData.Values["id"];
- return View();
- }
九、mvc定义可选URL段、可选参数
1、注册路由时定义可选URL段
2、通过Action参数来定义可选参数
十、mvc使用*来定义变长数量的URL段
Url段的数量
|
实例
|
Route映射
|
0
|
mydomain.com
|
controller = Home
action = Index
|
1
|
mydomain.com/Customer
|
controller = Customer
action = Index
|
2
|
mydomain.com/Customer/List
|
controller = Customer
action = List
|
3
|
mydomain.com/Customer/List/All
|
controller = Customer
action = List
id = All
|
4
|
mydomain.com/Customer/List/All/Delete
|
controller = Customer
action = List
id = All
catchall = Delete
|
5
|
mydomain.com/Customer/List/All/Delete/Perm
|
controller = Customer
action = List
id = All
catchall = Delete /Perm
|
十一、mvc使用命名空间来为路由的Controller类定优先级
- public static void RegisterRoutes(RouteCollection routes) {
- routes.MapRoute("Default",
- "{controller}/{action}/{id}",
- new { controller = "Home", action = "Index", id = UrlParameter.Optional },
- new string[] { "WebApplication1.Controllers" }
- );
- }
十二、mvc定义路由规则的约束
1、用正则表达式限制asp.net mvc路由规则
- public static void RegisterRoutes(RouteCollection routes) {
- routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
- new { controller = "Home", action = "Index", id = UrlParameter.Optional },
- new { controller = "^H.*"},
- new[] { "URLsAndRoutes.Controllers"});
- }
2、把asp.net mvc路由规则限制到到具体的值
- public static void RegisterRoutes(RouteCollection routes) {
- routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
- new { controller = "Home", action = "Index", id = UrlParameter.Optional },
- new { controller = "^H.*", action = "^Index$|^About$"},
- new[] { "URLsAndRoutes.Controllers"});
- }
3、把asp.net mvc路由规则限制到到提交请求方式(POST、GET)
- public static void RegisterRoutes(RouteCollection routes) {
- routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
- new { controller = "Home", action = "Index", id = UrlParameter.Optional },
- new { controller = "^H.*", action = "Index|About",
- httpMethod = new HttpMethodConstraint("GET") },
- new[] { "URLsAndRoutes.Controllers" });
- }
上面表示只匹配为GET方式的请求。
4、使用接口IRouteConstraint自定义一个asp.net mvc路由约束
- using System.Web;
- using System.Web.Routing;
- namespace URLsAndRoutes.Infrastructure {
- public class UserAgentConstraint : IRouteConstraint {
- private string requiredUserAgent;
- public UserAgentConstraint(string agentParam) {
- requiredUserAgent = agentParam;
- }
- public bool Match(HttpContextBase httpContext, Route route, string parameterName,
- RouteValueDictionary values, RouteDirection routeDirection) {
- return httpContext.Request.UserAgent != null &&
- httpContext.Request.UserAgent.Contains(requiredUserAgent);
- }
- }
- }
- public static void RegisterRoutes(RouteCollection routes) {
- routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
- new { controller = "Home", action = "Index", id = UrlParameter.Optional },
- new {
- controller = "^H.*", action = "Index|About",
- httpMethod = new HttpMethodConstraint("GET", "POST"),
- customConstraint = new UserAgentConstraint("IE")
- },
- new[] { "URLsAndRoutes.Controllers" });
- }
十三、mvc将URL路由到磁盘文件
- public static void RegisterRoutes(RouteCollection routes) {
- routes.RouteExistingFiles = true;
- routes.MapRoute("DiskFile", "Content/StaticContent.html",
- new {
- controller = "Account", action = "LogOn",
- },
- new {
- customConstraint = new UserAgentConstraint("IE")
- });
- routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
- new { controller = "Home", action = "Index", id = UrlParameter.Optional },
- new {
- controller = "^H.*", action = "Index|About",
- httpMethod = new HttpMethodConstraint("GET", "POST"),
- customConstraint = new UserAgentConstraint("IE")
- },
- new[] { "URLsAndRoutes.Controllers" });
- }
十四、mvc跳过、绕开路由系统设定
- public static void RegisterRoutes(RouteCollection routes) {
- routes.RouteExistingFiles = true;
- routes.MapRoute("DiskFile", "Content1/StaticContent.html",
- new {
- controller = "Account", action = "LogOn",
- },
- new {
- customConstraint = new UserAgentConstraint("IE")
- });
- routes.IgnoreRoute("Content/*{filename}");
- routes.MapRoute("", "{controller}/{action}");
- }