ASP.NET MVC4 路由的配置 十种方法
1.一般来说当学习到配置的时候我像个人对Asp.Net Mvc 已经有了一定的了解了。
路由是为了响应Http请求的产出物。在Mvc中Global.asax文件作出响应
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
//响应的路由配置文件
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
// RouteConfig 配置文件位于App_Start文件夹下
//RegisterRoutes为 系统默认生成
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 } //默认路径对应的 控制器/方法/参数 id = UrlParameter.Optional 意为默认不做指定
);
}
配置路由有两种方法(我已知的)
routes.Add() ;
routes.MapRoute();
routes.Add() ;是 实例方法
routes.MapRoute() 是RouteCollection的扩展方法
我这里采用的是routes.MapRoute() 方法 使用Add来调用比较复杂
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 }, // constraints: new // { // id = @"^\d*$", //使用正则使参数具体化 // httpMethod = new HttpMethodConstraint("get") // },// 限制访问方法 // namespaces: new[] { "MVC.Controllers" } //防止有重复 Home 与 Index 出现 //); //<二>---------------------------------------------- ----------------------- ----------------------- ----------------------- // routes.MapRoute("", "XM{controller}/{action}", new { controller = "Home", action = "Index" }, namespaces: new[] { "MVC.Controllers" }); //访问路径前加上 静态字段 //《三》---------------------------------------------- ----------------------- ----------------------- ----------------------- // routes.MapRoute("", "Home/{action}", new { controller = "Admin" }, namespaces: new[] { "MVC.Controllers" }); //更改文件不更改 访问名称 //<四>---------------------------------------------- ----------------------- ----------------------- ----------------------- // routes.MapRoute("", "Home/{action}", new { controller = "Admin", action = "Index" }, namespaces: new[] { "MVC.Controllers" }); //<五>---------------------------------------------- ----------------------- ----------------------- ----------------------- //routes.MapRoute("MyRoute", "{controller}/{action}/{id}",//MyRoute 以自己命名为准, // new // { // controller = "Home", // action = "Index", // id = "DefaultId" //也可以赋值为默认值 UrlParameter.Optional // });//给路径定义参数默认值 //取参数在该方法下面使用 Route.data["id"] 来取值 ,同样也可以参数取值 (string id) //<六>---------------------------------------------- ----------------------- ----------------------- ----------------------- // routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }); //*catchall 可以·获取·参数id 后面的所有“参数”/或“路径” //《七》---------------------------------------------- ----------------------- ----------------------- ----------------------- //routes.MapRoute("MyRoute", "{controller}/{action}/{id}", // new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // new { controller = "^H.*", action = "^Index$|^About$" } ); //使用正则 配置路由 { controller = "^H.*", action = "^Index$|^About$" } //controller 以H打头,action 只能是 Index); //<八>---------------------------------------------- ----------------------- ----------------------- ----------------------- // routes.MapRoute("", "Home/{action}", //new { controller = "Admin", action = "Index", httpMethod = new HttpMethodConstraint("get") },//限制访问方法 httpMethod = new HttpMethodConstraint("get") //namespaces: new[] { "MVC.Controllers" }); //《九》自定义路由限制-------------------------- ----------------------- ----------------------- ----------------------- ////1.第一种 直接在文件中配置完成,参数添加Chrome //routes.MapRoute("", "{controller}/{action}", // new { controller = "Home", action = "Index" }, // new { customConstraint = new MyRouteConstraint() }, namespaces: new[] { "MVC.Controllers" } //引入命名空间限制只有谷歌允许访问 添加配置文件 Filters /MyRouteConstraint 继承IRouteConstraint 必须实现方法 Match // ); ////2.第二种动态配置 在RouteConfig中 //routes.MapRoute("", "{controller}/{action}", // new { controller = "Home", action = "Index1" }, // new { customConstraint = new mySzlConstraint("Firefox") }, namespaces: new[] { "MVC.Controllers" } //引入命名空间限制只有火狐允许访问 添加配置文件 Filters/mySzlConstraint 继承IRouteConstraint 必须实现方法 Match // ); //routes.MapRoute("", "{controller}/{action}", // new { controller = "Admin", action = "Index" }, // namespaces: new[] { "MVC.Controllers" } //引入命名空间限制只有谷歌允许访问 // ); //《第十种》添加静态页面掩饰掉 真正的路由访问路径------------添加文件Commom/myPage-------------- ----------------------- ----------------------- ----------------------- //一. ////1.设置 routes.RouteExistingFiles = true; //routes.RouteExistingFiles = true; ////2.删除 IIS 配置文件,Control+F找到UrlRoutingModule-4.0,将这个节点的preCondition属性改为空 //routes.MapRoute("myPage", "Commom/myPage.html", // new { controller = "Home", action = "Index", }, namespaces: new[] { "MVC.Controllers" }); //二 //1.添加配置 routes.RouteExistingFiles = true; //2.设置 routes.IgnoreRoute routes.IgnoreRoute("Commom/{file}.html"); routes.MapRoute("", "Commom/myPage.html", new { controller = "Home", action = "Index5", }); }
外部添加的文件
《1》
public class MyRouteConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return httpContext.Request.UserAgent.Contains("Chrome");
}
}
《2》
public class mySzlConstraint : IRouteConstraint
{
private string myParam;//定义自己的参数 自己可访问
public mySzlConstraint(string agentParam)
{
myParam = agentParam; //构造函数
}
/// <summary>
/// 实现 方法Match
/// </summary>
/// <param name="httpContext"> 一个数据上下文</param>
/// <param name="route">定义路由</param>
/// <param name="parameterName"> </param>
/// <param name="values"></param>
/// <param name="routeDirection"></param>
/// <returns></returns>
public bool Match(HttpContextBase httpContext, Route route, string parameterName,
RouteValueDictionary values, RouteDirection routeDirection)
{
return httpContext.Request.UserAgent != null
&& httpContext.Request.UserAgent.Contains(myParam);
}