public GetVirtualPath ( System.Web.Routing.RequestContext requestContext, RouteValueDictionary values ) : System.Web.Routing.VirtualPathData | ||
requestContext | System.Web.Routing.RequestContext | |
values | RouteValueDictionary | |
return | System.Web.Routing.VirtualPathData |
public static string LinkTo(this UrlHelper urlHelper, Route route, RouteValueDictionary routeValues) { //TODO: remove code dupe see HtmlLinkToExtensions var requestCtx = urlHelper.RequestContext; var httpCtx = requestCtx.HttpContext; if (routeValues != null) { foreach (var d in route.Defaults) { if (!routeValues.ContainsKey(d.Key)) routeValues.Add(d.Key, d.Value); } } else { routeValues = route.Defaults; } VirtualPathData vpd = route.GetVirtualPath(requestCtx, routeValues); if (vpd == null) return null; return httpCtx.Request.ApplicationPath + vpd.VirtualPath; }
private static IHttpHandler GetRedirectHandler(RequestContext requestContext, string targetUrl, bool permanently) { string prefix = string.Empty; var slashPosition = targetUrl.IndexOf('/'); if (slashPosition >= 0) { prefix = targetUrl.Substring(0, slashPosition + 1); targetUrl = targetUrl.Substring(slashPosition + 1); } var route = new Route(targetUrl, null); var virtualPathData = route.GetVirtualPath(requestContext, requestContext.RouteData.Values); if (virtualPathData != null) { targetUrl = prefix + virtualPathData.VirtualPath; } return new DelegateHttpHandler(httpContext => Redirect(httpContext, targetUrl, permanently), false); }
public string GetClientUrl(string url, RouteValueDictionary values) { var site = _frontContext.Site; var subFolder = site.MatchedUrl.GetSubFolder(); if (!String.IsNullOrWhiteSpace(subFolder)) { url = subFolder + "/" + url; } var route = new Route(url, new MvcRouteHandler()); var data = route.GetVirtualPath(HttpContext.Current.Request.RequestContext, values); if (data == null) { return "/"; } return "/" + data.VirtualPath; }
private static string GenerateTargetLocation(this RequestContext context, string target, bool permanent) { if (target.StartsWith("~/", StringComparison.Ordinal)) { Route route = new Route(target.Substring(2), null); var vpd = route.GetVirtualPath(context, context.RouteData.Values); if (vpd != null) return string.Format(CultureInfo.InvariantCulture, "{0}/{1}", permanent ? string.Empty : "~", vpd.VirtualPath); } else if (target.StartsWith("/", StringComparison.Ordinal)) { Route route = new Route(target.Substring(1), null); var vpd = route.GetVirtualPath(context, context.RouteData.Values); if (null != vpd) return "/" + vpd.VirtualPath; } return target; }
/// <summary> /// Generates an outbound url from the specified values. /// </summary> /// <param name="routeValues">The route values from which to generate the url</param> /// <param name="route">The route to use to generate the url</param> /// <returns>The virtual path data object</returns> public static VirtualPathData GenerateOutboundVirtualPath(RouteValueDictionary routeValues, Route route) { HttpContextBase mockHttp = MockHelpers.HttpContext(null).Object; RequestContext context = new RequestContext(mockHttp, new RouteData()); // Act (generate a URL) return route.GetVirtualPath(context, routeValues); }
public void GetVirtualPath2 () { var r = new Route ("{foo}/{bar}", null); var hc = new HttpContextStub ("~/x/y", String.Empty); var rd = r.GetRouteData (hc); var vp = r.GetVirtualPath (new RequestContext (hc, rd), null); Assert.IsNotNull (vp, "#1"); Assert.AreEqual ("x/y", vp.VirtualPath, "#2"); Assert.AreEqual (r, vp.Route, "#3"); Assert.AreEqual (0, vp.DataTokens.Count, "#4"); }
public void GetVirtualPath () { var r = new Route ("foo/bar", null); var rd = new RouteData (); var vp = r.GetVirtualPath (new RequestContext (new HttpContextStub ("~/foo/bar"), rd), null); Assert.AreEqual ("foo/bar", vp.VirtualPath, "#1"); Assert.AreEqual (r, vp.Route, "#2"); vp = r.GetVirtualPath (new RequestContext (new HttpContextStub ("~/foo/bar/baz"), rd), null); Assert.AreEqual ("foo/bar", vp.VirtualPath, "#3"); Assert.AreEqual (r, vp.Route, "#4"); }
public void GetVirtualPathNullValues () { // null values is allowed. var r = new Route (null, null); var rd = new RouteData (); var vp = r.GetVirtualPath (new RequestContext (new HttpContextStub (), rd), null); Assert.AreEqual (String.Empty, vp.VirtualPath, "#1"); Assert.AreEqual (r, vp.Route, "#2"); }
public void GetVirtualPathNullContext () { try { var r = new Route (null, null); r.GetVirtualPath (null, new RouteValueDictionary ()); } catch (NullReferenceException) { // .NET lacks null arg check here. (No need to mimic silly behavior here.) throw new ArgumentNullException (); } }
public void GetVirtualPath19 () { var context = new HttpContextWrapper ( new HttpContext (new HttpRequest ("filename", "http://localhost/filename", String.Empty), new HttpResponse (new StringWriter()) ) ); var rc = new RequestContext (context, new RouteData ()); var route = new Route ("HelloWorld", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new {controller = "Home", action = "HelloWorld"}) }; var lowercase = route.GetVirtualPath (rc, new RouteValueDictionary { {"controller", "home"}, {"action", "helloworld"} }); var standardCase = route.GetVirtualPath (rc, new RouteValueDictionary { {"controller", "Home"}, {"action", "HelloWorld"} }); var uppercase = route.GetVirtualPath (rc, new RouteValueDictionary { {"controller", "HOME"}, {"action", "HELLOWORLD"} }); Assert.IsNotNull(lowercase, "#A1"); Assert.AreEqual ("HelloWorld", lowercase.VirtualPath, "#A2"); Assert.IsNotNull(standardCase, "#A3"); Assert.AreEqual ("HelloWorld", standardCase.VirtualPath, "#A4"); Assert.IsNotNull(uppercase, "#A5"); Assert.AreEqual ("HelloWorld", uppercase.VirtualPath, "#A6"); }
public void GetVirtualPath16 () { var context = new HttpContextWrapper ( new HttpContext (new HttpRequest ("filename", "http://localhost/filename", String.Empty), new HttpResponse (new StringWriter()) ) ); var rc = new RequestContext (context, new RouteData ()); var route = new Route ("Hello", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new {controller = "Home", action = "Hello", page = 1}) }; var routeValues = new RouteValueDictionary { {"controller", "Home"}, {"action", "Hello"}, {"page", 1} }; var result = route.GetVirtualPath(rc, routeValues); Assert.IsNotNull(result, "#A1"); Assert.AreEqual("Hello", result.VirtualPath, "#A2"); }
public IHttpHandler GetHttpHandler(RequestContext requestContext) { if (_redirectParams.RedirectTo.StartsWith("~/")) { string virtualPath = _redirectParams.RedirectTo.Substring(2); Route route = new Route(virtualPath, null); var vpd = route.GetVirtualPath(requestContext, requestContext.RouteData.Values); if (vpd != null) { _redirectParams.RedirectTo = "~/" + vpd.VirtualPath; } } return new RedirectHandler(_redirectParams, false); }