Skip to content

Commit

Permalink
Added support for eliminating the ambient request values from being c…
Browse files Browse the repository at this point in the history
…onsidered when resolving URLs (maartenba#213).
  • Loading branch information
NightOwl888 committed Mar 3, 2014
1 parent 9351296 commit 47008d0
Show file tree
Hide file tree
Showing 5 changed files with 385 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ namespace MvcSiteMapProvider.Web.Mvc
public interface IMvcContextFactory
{
HttpContextBase CreateHttpContext();
HttpContextBase CreateHttpContext(ISiteMapNode node, Uri nodeUri, TextWriter writer);
HttpContextBase CreateHttpContext(ISiteMapNode node, Uri uri, TextWriter writer);
RequestContext CreateRequestContext(ISiteMapNode node, RouteData routeData);
RequestContext CreateRequestContext();
RequestContext CreateRequestContext(HttpContextBase httpContext);
RequestContext CreateRequestContext(HttpContextBase httpContext, RouteData routeData);
ControllerContext CreateControllerContext(RequestContext requestContext, ControllerBase controller);
IRequestCache GetRequestCache();
RouteCollection GetRoutes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ protected virtual HttpContextBase CreateHttpContext(ISiteMapNode node)
return new SiteMapHttpContext(HttpContext.Current, node);
}

public virtual HttpContextBase CreateHttpContext(ISiteMapNode node, Uri nodeUri, TextWriter writer)
public virtual HttpContextBase CreateHttpContext(ISiteMapNode node, Uri uri, TextWriter writer)
{
if (node == null)
throw new ArgumentNullException("node");
if (nodeUri == null)
throw new ArgumentNullException("nodeUri");
if (uri == null)
throw new ArgumentNullException("uri");
if (writer == null)
throw new ArgumentNullException("writer");

var request = new HttpRequest(string.Empty, nodeUri.ToString(), nodeUri.Query);
var request = new HttpRequest(string.Empty, uri.ToString(), uri.Query);
var response = new HttpResponse(writer);
var httpContext = new HttpContext(request, response);
return new SiteMapHttpContext(httpContext, node);
Expand All @@ -61,6 +61,16 @@ public virtual RequestContext CreateRequestContext()
return new RequestContext(httpContext, new RouteData());
}

public virtual RequestContext CreateRequestContext(HttpContextBase httpContext)
{
return new RequestContext(httpContext, new RouteData());
}

public virtual RequestContext CreateRequestContext(HttpContextBase httpContext, RouteData routeData)
{
return new RequestContext(httpContext, routeData);
}

public virtual ControllerContext CreateControllerContext(RequestContext requestContext, ControllerBase controller)
{
if (requestContext == null)
Expand Down Expand Up @@ -88,13 +98,13 @@ public virtual RouteCollection GetRoutes()

public virtual IUrlHelper CreateUrlHelper(RequestContext requestContext)
{
return new UrlHelperAdapter(requestContext);
return new UrlHelperAdapter(requestContext, this.GetRoutes());
}

public virtual IUrlHelper CreateUrlHelper()
{
var requestContext = this.CreateRequestContext();
return new UrlHelperAdapter(requestContext);
return new UrlHelperAdapter(requestContext, this.GetRoutes());
}

public virtual AuthorizationContext CreateAuthorizationContext(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ namespace MvcSiteMapProvider.Web
public class UrlHelperAdapter
: UrlHelper, IUrlHelper
{
public UrlHelperAdapter(RequestContext requestContext)
private UrlHelperAdapter(RequestContext requestContext)
: base(requestContext)
{
}

private UrlHelperAdapter(RequestContext requestContext, RouteCollection routeCollection)
public UrlHelperAdapter(RequestContext requestContext, RouteCollection routeCollection)
: base(requestContext, routeCollection)
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Routing;
using MvcSiteMapProvider.Web.Mvc;
Expand Down Expand Up @@ -42,7 +43,7 @@ IUrlPath urlPath
/// <returns>The resolved URL.</returns>
public override string ResolveUrl(ISiteMapNode node, string area, string controller, string action, IDictionary<string, object> routeValues)
{
if (!String.IsNullOrEmpty(node.UnresolvedUrl))
if (!string.IsNullOrEmpty(node.UnresolvedUrl))
{
return this.ResolveVirtualPath(node);
}
Expand All @@ -63,8 +64,28 @@ protected virtual string ResolveVirtualPath(ISiteMapNode node)

protected virtual string ResolveRouteUrl(ISiteMapNode node, string area, string controller, string action, IDictionary<string, object> routeValues)
{
string result = String.Empty;
var urlHelper = mvcContextFactory.CreateUrlHelper();
string result = string.Empty;
// Create a TextWriter with null stream as a backing stream
// which doesn't consume resources
using (var nullWriter = new StreamWriter(Stream.Null))
{
var requestContext = this.CreateRequestContext(node, true, nullWriter);
result = this.ResolveRouteUrl(node, area, controller, action, routeValues, requestContext);
}

if (string.IsNullOrEmpty(result))
{
// fixes #115 - UrlResolver should not throw exception.
return urlPath.MakeVirtualPathAppAbsolute(urlPath.Combine(urlPath.AppDomainAppVirtualPath, "~/")) + Guid.NewGuid().ToString();
}

return result;
}

protected virtual string ResolveRouteUrl(ISiteMapNode node, string area, string controller, string action, IDictionary<string, object> routeValues, RequestContext requestContext)
{
string result = string.Empty;
var urlHelper = this.mvcContextFactory.CreateUrlHelper(requestContext);
var routeValueDictionary = new RouteValueDictionary(routeValues);

if (!string.IsNullOrEmpty(node.Route))
Expand All @@ -77,13 +98,29 @@ protected virtual string ResolveRouteUrl(ISiteMapNode node, string area, string
result = urlHelper.Action(action, controller, routeValueDictionary);
}

if (string.IsNullOrEmpty(result))
return result;
}

protected virtual HttpContextBase CreateHttpContext(ISiteMapNode node, TextWriter writer)
{
var currentHttpContext = this.mvcContextFactory.CreateHttpContext();

// Create a URI with the home page and no query string values.
var uri = new Uri(currentHttpContext.Request.Url, "/");
return this.mvcContextFactory.CreateHttpContext(node, uri, writer);
}

protected virtual RequestContext CreateRequestContext(ISiteMapNode node, bool includeAmbientRequestValues, TextWriter writer)
{
if (!includeAmbientRequestValues)
{
// fixes #115 - UrlResolver should not throw exception.
return urlPath.MakeVirtualPathAppAbsolute(urlPath.Combine(urlPath.AppDomainAppVirtualPath, "~/")) + Guid.NewGuid().ToString();
var httpContext = this.CreateHttpContext(node, writer);
return this.mvcContextFactory.CreateRequestContext(httpContext);
}
else
{
return this.mvcContextFactory.CreateRequestContext();
}

return result;
}
}
}
Loading

0 comments on commit 47008d0

Please sign in to comment.