private static void TryResolveRoutablePath(HttpApplication app) { var context = app.Context; var request = context?.Request; if (request == null) { return; } if (_routes.Count > 0) { var path = request.AppRelativeCurrentExecutionFilePath.TrimStart('~').TrimEnd('/'); var method = request.HttpMethod.EmptyNull(); foreach (var route in _routes) { if (route.PathPattern.IsMatch(path) && route.HttpMethodPattern.IsMatch(method)) { var module = new UrlRoutingModule(); module.PostResolveRequestCache(new HttpContextWrapper(context)); return; } } } }
public void PostMapRequestHandlerNoMatch() { var m = new UrlRoutingModule(); RouteTable.Routes.Add(new MyRoute(null, null)); m.PostMapRequestHandler(new HttpContextStub2()); }
public void PostResolveRequestCacheNullHttpHandler() { var m = new UrlRoutingModule(); RouteTable.Routes.Add(new MyRoute("foo/bar", new NullRouteHandler())); m.PostResolveRequestCache(new HttpContextStub2("~/foo/bar", null)); }
/// <summary> /// Rewrites to the Umbraco handler - we always send the request via our MVC rendering engine, this will deal with /// requests destined for webforms. /// </summary> /// <param name="context"></param> /// <param name="pcr"> </param> private void RewriteToUmbracoHandler(HttpContextBase context, PublishedRequest pcr) { // NOTE: we do not want to use TransferRequest even though many docs say it is better with IIS7, turns out this is // not what we need. The purpose of TransferRequest is to ensure that .net processes all of the rules for the newly // rewritten URL, but this is not what we want! // read: http://forums.iis.net/t/1146511.aspx var query = pcr.Uri.Query.TrimStart('?'); // GlobalSettings.Path has already been through IOHelper.ResolveUrl() so it begins with / and vdir (if any) var rewritePath = _globalSettings.Path.TrimEnd('/') + "/RenderMvc"; // rewrite the path to the path of the handler (i.e. /umbraco/RenderMvc) context.RewritePath(rewritePath, "", query, false); //if it is MVC we need to do something special, we are not using TransferRequest as this will //require us to rewrite the path with query strings and then re-parse the query strings, this would //also mean that we need to handle IIS 7 vs pre-IIS 7 differently. Instead we are just going to create //an instance of the UrlRoutingModule and call it's PostResolveRequestCache method. This does: // * Looks up the route based on the new rewritten URL // * Creates the RequestContext with all route parameters and then executes the correct handler that matches the route //we also cannot re-create this functionality because the setter for the HttpContext.Request.RequestContext is internal //so really, this is pretty much the only way without using Server.TransferRequest and if we did that, we'd have to rethink //a bunch of things! var urlRouting = new UrlRoutingModule(); urlRouting.PostResolveRequestCache(context); }
public void Pipeline3() { var m = new UrlRoutingModule(); RouteTable.Routes.Add(new MyRoute("{foo}/{bar}", new MyRouteHandler())); #if NET_4_0 var hc = new HttpContextStub5("~/x/y", String.Empty, "apppath"); #else var hc = new HttpContextStub2("~/x/y", String.Empty, "apppath"); #endif hc.SetResponse(new HttpResponseStub(2)); #if NET_4_0 Assert.IsNull(m.RouteCollection.GetRouteData(hc), "#0"); #else Assert.IsNotNull(m.RouteCollection.GetRouteData(hc), "#0"); m.PostResolveRequestCache(hc); try { m.PostMapRequestHandler(hc); Assert.Fail("#1"); } catch (ApplicationException ex) { Assert.AreEqual("~/UrlRouting.axd", ex.Message, "#2"); } #endif }
public virtual void PostResolveRequestCache(HttpContextBase context) { var request = context?.Request; if (request == null) { return; } if (_routes.Count > 0) { var path = request.AppRelativeCurrentExecutionFilePath.TrimStart('~').TrimEnd('/'); var method = request.HttpMethod.EmptyNull(); foreach (var route in _routes) { if (route.PathPattern.IsMatch(path) && route.HttpMethodPattern.IsMatch(method)) { var module = new UrlRoutingModule(); module.PostResolveRequestCache(context); return; } } } }
public void PostMapRequestHandlerNoPath() { var m = new UrlRoutingModule(); RouteTable.Routes.Add(new MyRoute("foo/bar", new MyRouteHandler())); // ... huh? no NIE? what does it do then? m.PostMapRequestHandler(new HttpContextStub2("~/foo/bar", null)); }
public void PostResolveRequestCacheNoPath() { var m = new UrlRoutingModule(); RouteTable.Routes.Add(new MyRoute("foo/bar", new MyRouteHandler())); // it tries to get HttpContextBase.Request.Path and causes NIE. m.PostResolveRequestCache(new HttpContextStub2("~/foo/bar", null)); }
public void PostResolveRequestCacheStopRoutingHttpHandler() { var m = new UrlRoutingModule(); RouteTable.Routes.Add(new MyRoute("foo/bar", new StopRoutingHandler())); var hc = new HttpContextStub3("~/foo/bar", String.Empty, "apppath", false); m.PostResolveRequestCache(hc); Assert.IsNull(hc.RewrittenPath, "StopRoutingHandler should stop before the path is rewritten"); }
public void Pipeline3() { var m = new UrlRoutingModule(); RouteTable.Routes.Add(new MyRoute("{foo}/{bar}", new MyRouteHandler())); var hc = new HttpContextStub5("~/x/y", String.Empty, "apppath"); hc.SetResponse(new HttpResponseStub(2)); Assert.IsNull(m.RouteCollection.GetRouteData(hc), "#0"); }
public void SetRouteCollectionNull() { var m = new UrlRoutingModule(); RouteTable.Routes.Add(new Route(null, null)); Assert.IsNotNull(m.RouteCollection, "#1"); Assert.AreEqual(RouteTable.Routes, m.RouteCollection, "#1-2"); m.RouteCollection = null; Assert.IsNotNull(m.RouteCollection, "#2"); Assert.AreEqual(RouteTable.Routes, m.RouteCollection, "#2-2"); }
public void PostResolveRequestCache() { var m = new UrlRoutingModule(); RouteTable.Routes.Add(new MyRoute("{foo}/{bar}", new MyRouteHandler())); var hc = new HttpContextStub3("~/x/y", "z", "apppath", false); hc.SetResponse(new HttpResponseStub(2)); m.PostResolveRequestCache(hc); Assert.AreEqual("~/UrlRouting.axd", hc.RewrittenPath, "#1"); // it internally stores the handler }
public void Pipeline1() { var m = new UrlRoutingModule(); RouteTable.Routes.Add(new MyRoute("{foo}/{bar}", new MyRouteHandler())); var hc = new HttpContextStub3("~/x/y", "z", "apppath", false); hc.SetResponse(new HttpResponseStub(2)); m.PostResolveRequestCache(hc); Assert.AreEqual("~/UrlRouting.axd", hc.RewrittenPath, "#1"); // It tries to set Handler and causes NIE m.PostMapRequestHandler(hc); }
/// <summary> /// Rewrites to the correct Umbraco handler, either WebForms or Mvc /// </summary> /// <param name="context"></param> /// <param name="pcr"> </param> private static void RewriteToUmbracoHandler(HttpContextBase context, PublishedContentRequest pcr) { // NOTE: we do not want to use TransferRequest even though many docs say it is better with IIS7, turns out this is // not what we need. The purpose of TransferRequest is to ensure that .net processes all of the rules for the newly // rewritten url, but this is not what we want! // read: http://forums.iis.net/t/1146511.aspx string query = pcr.Uri.Query.TrimStart(new[] { '?' }); string rewritePath; if (pcr.RenderingEngine == RenderingEngine.Unknown) { // Unkwnown means that no template was found. Default to Mvc because Mvc supports hijacking // routes which sometimes doesn't require a template since the developer may want full control // over the rendering. Can't do it in WebForms, so Mvc it is. And Mvc will also handle what to // do if no template or hijacked route is exist. pcr.RenderingEngine = RenderingEngine.Mvc; } switch (pcr.RenderingEngine) { case RenderingEngine.Mvc: // GlobalSettings.Path has already been through IOHelper.ResolveUrl() so it begins with / and vdir (if any) rewritePath = GlobalSettings.Path.TrimEnd(new[] { '/' }) + "/RenderMvc"; // rewrite the path to the path of the handler (i.e. /umbraco/RenderMvc) context.RewritePath(rewritePath, "", query, false); //if it is MVC we need to do something special, we are not using TransferRequest as this will //require us to rewrite the path with query strings and then reparse the query strings, this would //also mean that we need to handle IIS 7 vs pre-IIS 7 differently. Instead we are just going to create //an instance of the UrlRoutingModule and call it's PostResolveRequestCache method. This does: // * Looks up the route based on the new rewritten URL // * Creates the RequestContext with all route parameters and then executes the correct handler that matches the route //we also cannot re-create this functionality because the setter for the HttpContext.Request.RequestContext is internal //so really, this is pretty much the only way without using Server.TransferRequest and if we did that, we'd have to rethink //a bunch of things! var urlRouting = new UrlRoutingModule(); urlRouting.PostResolveRequestCache(context); break; case RenderingEngine.WebForms: rewritePath = "~/default.aspx"; // rewrite the path to the path of the handler (i.e. default.aspx) context.RewritePath(rewritePath, "", query, false); break; default: throw new Exception("Invalid RenderingEngine."); } }
public void PostResolveRequestCacheCallRewritePath() { var m = new UrlRoutingModule(); RouteTable.Routes.Add(new MyRoute("{foo}/{bar}", new MyRouteHandler())); var hc = new HttpContextStub2("~/x/y", "z"); try { m.PostResolveRequestCache(hc); Assert.Fail("#1"); } catch (ApplicationException ex) { Assert.AreEqual("~/UrlRouting.axd", ex.Message, "#2"); } }
public void PostResolveRequestCachePathToExistingFile() { var m = new UrlRoutingModule(); RouteTable.Routes.Add(new MyRoute("~/{foo}/{bar}", new MyRouteHandler())); var hc = new HttpContextStub2("~/hoge/fuga", String.Empty, "."); // it tries to get HttpContextBase.Response, so set it. hc.SetResponse(new HttpResponseStub(3)); try { m.PostResolveRequestCache(hc); Assert.Fail("#1"); } catch (ApplicationException ex) { Assert.AreEqual("~/UrlRouting.axd", ex.Message, "#2"); } }
/// <summary> /// Rewrites to the correct Umbraco handler, either WebForms or Mvc /// </summary> /// <param name="context"></param> /// <param name="currentQuery"></param> /// <param name="engine"> </param> private void RewriteToUmbracoHandler(HttpContext context, string currentQuery, RenderingEngine engine) { //NOTE: We do not want to use TransferRequest even though many docs say it is better with IIS7, turns out this is //not what we need. The purpose of TransferRequest is to ensure that .net processes all of the rules for the newly //rewritten url, but this is not what we want! // http://forums.iis.net/t/1146511.aspx string rewritePath; switch (engine) { case RenderingEngine.Mvc: //the Path is normally ~/umbraco but we need to remove the start ~/ of it and if someone modifies this //then we should be rendering the MVC stuff in that location. rewritePath = "~/" + GlobalSettings.Path.TrimStart(new[] { '~', '/' }).TrimEnd(new[] { '/' }) + "/RenderMvc"; // we rewrite the path to the path of the handler (i.e. default.aspx or /umbraco/RenderMvc ) context.RewritePath(rewritePath, "", currentQuery.TrimStart(new[] { '?' }), false); //if it is MVC we need to do something special, we are not using TransferRequest as this will //require us to rewrite the path with query strings and then reparse the query strings, this would //also mean that we need to handle IIS 7 vs pre-IIS 7 differently. Instead we are just going to create //an instance of the UrlRoutingModule and call it's PostResolveRequestCache method. This does: // * Looks up the route based on the new rewritten URL // * Creates the RequestContext with all route parameters and then executes the correct handler that matches the route //we also cannot re-create this functionality because the setter for the HttpContext.Request.RequestContext is internal //so really, this is pretty much the only way without using Server.TransferRequest and if we did that, we'd have to rethink //a bunch of things! var urlRouting = new UrlRoutingModule(); urlRouting.PostResolveRequestCache(new HttpContextWrapper(context)); break; case RenderingEngine.WebForms: default: rewritePath = "~/default.aspx"; // rewrite the path to the path of the handler (i.e. default.aspx or /umbraco/RenderMvc ) context.RewritePath(rewritePath, "", currentQuery.TrimStart(new[] { '?' }), false); break; } }
public void Pipeline2() { var m = new UrlRoutingModule(); RouteTable.Routes.Add(new MyRoute("{foo}/{bar}", new MyRouteHandler())); #if NET_4_0 var hc = new HttpContextStub4("~/x/y", "z", "apppath", true); #else var hc = new HttpContextStub3("~/x/y", "z", "apppath", true); #endif hc.HttpHandler = new MyHttpHandler(); hc.SetResponse(new HttpResponseStub(2)); m.PostResolveRequestCache(hc); #if NET_4_0 Assert.AreEqual(null, hc.RewrittenPath, "#1"); #else Assert.AreEqual("~/UrlRouting.axd", hc.RewrittenPath, "#1"); #endif // It tries to set Handler and causes NIE m.PostMapRequestHandler(hc); }
/// <summary> /// Rewrites to the default back office page. /// </summary> /// <param name="context"></param> private void RewriteToBackOfficeHandler(HttpContextBase context) { // GlobalSettings.Path has already been through IOHelper.ResolveUrl() so it begins with / and vdir (if any) var rewritePath = _globalSettings.Path.TrimEnd('/') + "/Default"; // rewrite the path to the path of the handler (i.e. /umbraco/RenderMvc) context.RewritePath(rewritePath, "", "", false); //if it is MVC we need to do something special, we are not using TransferRequest as this will //require us to rewrite the path with query strings and then re-parse the query strings, this would //also mean that we need to handle IIS 7 vs pre-IIS 7 differently. Instead we are just going to create //an instance of the UrlRoutingModule and call it's PostResolveRequestCache method. This does: // * Looks up the route based on the new rewritten URL // * Creates the RequestContext with all route parameters and then executes the correct handler that matches the route //we also cannot re-create this functionality because the setter for the HttpContext.Request.RequestContext is internal //so really, this is pretty much the only way without using Server.TransferRequest and if we did that, we'd have to rethink //a bunch of things! var urlRouting = new UrlRoutingModule(); urlRouting.PostResolveRequestCache(context); }