Global.asax 是 程序入口文件
路由配置 为什么localhost:8088/Home/Index/1 能返问到我们写的
会去掉前缀跟端口号 变成Home/Index/1 用这个跟路由规则进行匹配
**************路由扩展的三大层面
1. 直接用routers.MapRoute进行扩展 (这里定义的规则在源码中就是生成一个Route)
2. 扩展Route
2.1 创建一个扩展规则 添加到RouteData中 这个对象就是存放的路由规则
只要发起请求的浏览器版本为这个的 给他指定路由规则
public class MyRoute : RouteBase
{
public override RouteData GetRouteData(HttpContextBase httpContext)
{
if (httpContext.Request.UserAgent.IndexOf("Chrome71.0.3578.98") >= )
{
RouteData rd = new RouteData(this, new MvcRouteHandler());
rd.Values.Add("controller", "Jcb");
rd.Values.Add("action", "Index");
return rd;
}
return null;
} public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
//这里要改成返回null 不然会报错
return null;
}
}
2.2在RouteConfig中把对象添加到RouteCollection中
/// <summary>
/// 路由配置
/// </summary>
public class RouteConfig
{
public static void RegisterMyRoutes(RouteCollection routes)
{
routes.Add(new MyRoute());
}
2.3在Globl启动配置中注册路由
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();//注册区域路由
GlobalConfiguration.Configure(WebApiConfig.Register);//注册webapi路由
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);// RouteConfig.RegisterMyMVCHandler(RouteTable.Routes);
RouteConfig.RegisterMyRoutes(RouteTable.Routes);
RouteConfig.RegisterRoutes(RouteTable.Routes);//注册路由
BundleConfig.RegisterBundles(BundleTable.Bundles);
ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory());//替换默认的控制器工厂 //IOCConfig.InitIOCContainer();
}
3.扩展Handler 替换掉MVC框架中的MVCHandler
3.1.配置扩展的Handler
只要访问的地址 控制器是Jcb 就使用自定义的Handler来处理这次请求 之间不用MVC内置的Handler
/// <summary>
/// 扩展IRouteHandler,
/// 扩展IHttpHandler
/// </summary>
public class ElevenRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return new ElevenHttpHandler(requestContext);
}
} /// <summary>
/// 还是我们熟悉的handler
/// </summary>
public class ElevenHttpHandler : IHttpHandler
{
public ElevenHttpHandler(RequestContext requestContext)
{
Console.WriteLine("构造我的Handler");
} public void ProcessRequest(HttpContext context)
{
string url = context.Request.Url.AbsoluteUri;
context.Response.Write(string.Format("这里是我的定制:{0}", this.GetType().Name));
context.Response.Write((string.Format("当前地址为:{0}", url))); context.Response.End();
} public virtual bool IsReusable
{
get
{
return false;
}
}
}
3.2.在RouteConfig配置中添加
public static void RegisterMyMVCHandler(RouteCollection routes)
{
routes.Add(new Route("Jcb/{*Info}", new ElevenRouteHandler()
));
}
3.3在Globl中注册
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();//注册区域路由
GlobalConfiguration.Configure(WebApiConfig.Register);//注册webapi路由
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);// RouteConfig.RegisterMyMVCHandler(RouteTable.Routes);
RouteConfig.RegisterMyRoutes(RouteTable.Routes);
RouteConfig.RegisterRoutes(RouteTable.Routes);//注册路由
BundleConfig.RegisterBundles(BundleTable.Bundles);
ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory());//替换默认的控制器工厂 //IOCConfig.InitIOCContainer();
}