private void Context_PostResolveRequestCache(object sender, EventArgs e) { var app = sender as HttpApplication; HttpContextBase context = new HttpContextWrapper(app.Context); var routeData = RouteTable.Routes.GetRouteData(context); //无路由则不进行处理 if (routeData == null) { return; } var requestContext = new RequestContext { RouteData = routeData, HttpContext = context }; var httphandler = routeData.RouteHandler?.GetHttpHandler(requestContext); if (httphandler == null) { throw new InvalidOperationException(); } //重新映射 context.RemapHandler(httphandler); }
protected virtual void OnPostResolveRequestCache(object sender, EventArgs e) { // 下面是得到当前请求的 Http 上下文 HttpContext currentHttpContext = ((HttpApplication)(sender)).Context; // 下面是一个包装类,把 HttpContext 转换成 HttpContextBase。如果你问我为什么 // HttpApplication.Context 返回的是 HttpContext,而不是 HttpContextBase,我 // 只能说一句:历史遗留问题 HttpContextWrapper httpContext = new HttpContextWrapper(currentHttpContext); // 下面是从当前 HttpContextBase 中获取 URL,把这个 URL 和应用程序启动( // Application_Start )中注册的静态路由表(RouteTable)相比较(也称匹配), // 第一个匹配的 RouteData 就立即返回,如果都没有匹配,则返回 NULL。 RouteData routeData = RouteTable.Routes.GetRouteData(httpContext); if (null == routeData) { return; } RequestContext requestContext = new RequestContext { RouteData = routeData, HttpContext = httpContext }; IHttpHandler handler = routeData.RouteHandler.GetHttpHandler(requestContext); httpContext.RemapHandler(handler); // 用于为请求指定处理程序。 }
internal static bool RemapHandlerForBundleRequests(HttpApplication app) { HttpContextBase context = new HttpContextWrapper(app.Context); // Don't block requests to existing files or directories string requestPath = context.Request.AppRelativeCurrentExecutionFilePath; VirtualPathProvider vpp = HostingEnvironment.VirtualPathProvider; if (vpp.FileExists(requestPath) || vpp.DirectoryExists(requestPath)) { return(false); } string bundleRequestPath = GetBundleUrlFromContext(context); // Check if this request matches a bundle in the app Bundle requestBundle = BundleTable.Bundles.GetBundleFor(bundleRequestPath); if (requestBundle != null) { context.RemapHandler(new BundleHandler(requestBundle, bundleRequestPath)); return(true); } return(false); }
/// <summary> /// Handles the PostResolveRequestCache event of the context control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void context_PostResolveRequestCache(object sender, EventArgs e) { var context = new HttpContextWrapper(((HttpApplication)sender).Context); // check to see if this is a proxy request if (context.Items.Contains(Manager.ProxyHandlerStorageName)) { var proxyHandler = context.Items[Manager.ProxyHandlerStorageName] as IHttpHandler; context.RemapHandler(proxyHandler); } }
private static async void OnApplicationOnPostAuthenticateRequest(object sender, EventArgs e) { var context = new HttpContextWrapper(HttpContext.Current); IRequestFilter[] requestFilters = _configuration.RequestFilters.ToArray(); if (!requestFilters.Any()) { context.RemapHandler(_configuration.HttpHandler); return; } foreach (IRequestFilter requestFilter in _configuration.RequestFilters) { if ((await requestFilter.FilterAsync(context)).ResultType == FilterResultType.UseJuniorRouteHandler) { context.RemapHandler(_configuration.HttpHandler); return; } } }
protected virtual void OnPostResolveRequestCache(object sender, EventArgs e) { HttpContextWrapper httpContext = new HttpContextWrapper(HttpContext.Current); RouteData routeData = RouteTable.Routes.GetRouteData(httpContext); if (null == routeData) { return; } RequestContext requestContext = new RequestContext { RouteData = routeData, HttpContext = httpContext }; IHttpHandler handler = routeData.RouteHandler.GetHttpHandler(requestContext); httpContext.RemapHandler(handler); }
private void Context_PostResolveRequestCache(object sender, EventArgs e) { var httpContext = new HttpContextWrapper(HttpContext.Current); RouteData routeData = RouteTable.Routes.GetRouteData(httpContext); if (routeData == null) { return; } var requestContext = new RequestContext() { HttpContext = httpContext, RouteData = routeData }; httpContext.RemapHandler(routeData.RouteHandler.GetHttpHandler(requestContext)); }
public virtual void OnPostResolveRequestCache(object sender, EventArgs e) { HttpContextWrapper wrapper = new HttpContextWrapper(HttpContext.Current); RouteData routeData = RouteTable.Routes.GetRouteData(wrapper); if (routeData == null) { return; } var requestContext = new RequestContext(wrapper, routeData); var routeHandler = routeData.RouteHandler?.GetHttpHandler(requestContext); if (routeHandler == null) { return; } wrapper.RemapHandler(routeHandler); }
protected void OnPostResolveRequestCache(object sender, EventArgs e) { //HttpContextWrapper是HttpContext的子类 HttpContextWrapper httpContext = new HttpContextWrapper(HttpContext.Current); //RouteTable的静态只读方法Routes得到表示全局路由表的RouteDictionary对象, //调用GetRouteData方法并传入用于封装当前HttpContext的HttpContextWrapper对象, //最终得到一个封装路由数据的RouteData对象 RouteData routeData = RouteTable.Routes.GetRouteData(httpContext); if (routeData == null) { return; } RequestContext requestContext = new RequestContext { HttpContext = httpContext, RouteData = routeData }; IHttpHandler handler = routeData.RouteHandler.GetHttpHandler(requestContext); httpContext.RemapHandler(handler); //用于当前的HTTP请求的处理 }
public void Init(HttpApplication context) { // 下面是如何处理 LogRequest 事件并为其 // 提供自定义日志记录实现的示例 context.LogRequest += (source, e) => { }; context.BeginRequest += (sender, e) => { }; context.AcquireRequestState += (sender, e) => { var application = (HttpApplication)sender; var ctx = application.Context; Log.Info(ctx.Request.HttpMethod + " " + ctx.Request.Url.ToString()); try { var path = ctx.Request.Path.ToLower(); if (path.EndsWith(".css") || path.EndsWith(".js") || path.EndsWith(".ico") || path.EndsWith(".task") || path.EndsWith(".sync") || path.EndsWith(".jpg") || path.EndsWith(".png") || path.EndsWith(".ttf") || path.EndsWith(".wcapp")) { return; } if (path == "/" || path == "/home/index" || path.Contains(".asmx")) { return; } if (ctx.Session.GetSessionUser() != null) { return; } if (!path.Contains("pdm")) { ctx.Response.Redirect("~/"); return; } var usrid = ctx.Request.QueryString["authorId"]; if (usrid.IsEmpty()) { ctx.Response.Redirect("~/"); return; } var db = new Context(); var usr = db.Users.Include(x => x.AuthorityRs).SingleOrDefault(x => x.Id == usrid); if (usr == null) { ctx.Response.Redirect("~/"); return; } ctx.Session.SetSessionUser(new SessionUser { Id = usr.Id, Name = usr.Name, Authorities = usr.Authorities }); } catch (Exception) { ctx.Response.Write("系统出现错误,请稍后访问......"); } }; context.PostResolveRequestCache += (sender, e) => { var ctx = new HttpContextWrapper(((HttpApplication)sender).Context); IHttpHandler handler = null; var path = context.Request.Path.ToLower(); if (handler != null) { ctx.RemapHandler(handler); } }; context.ReleaseRequestState += (sender, e) => { }; }