public virtual void PostResolveRequestCache(HttpContextBase context) { var request = context.Request; if (request == null || _routes.Count == 0) return; 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; } } }
/// <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 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 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 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); }
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 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"); } }
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); }
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 PostResolveRequestCache () { 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", false); #else var hc = new HttpContextStub3 ("~/x/y", "z", "apppath", false); #endif 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 internally stores the handler }
public void PostResolveRequestCacheModifiedPath () { var m = new UrlRoutingModule (); RouteTable.Routes.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ())); var hc = new HttpContextStub2 ("~/x/y", "z", "apppath"); hc.SetResponse (new HttpResponseStub (2)); try { m.PostResolveRequestCache (hc); Assert.Fail ("#1"); } catch (ApplicationException ex) { Assert.AreEqual ("~/UrlRouting.axd", ex.Message, "#2"); } }
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)); }
/// <summary> /// Rewrites to the default back office page. /// </summary> /// <param name="context"></param> private static 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(new[] { '/' }) + "/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 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); }