-
-
Save prabirshrestha/1051471 to your computer and use it in GitHub Desktop.
AppHarbor URL handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web; | |
namespace NetflixDemo | |
{ | |
public class AppHarborHttpContextWrapper : HttpContextWrapper | |
{ | |
private readonly HttpContext httpContext; | |
public AppHarborHttpContextWrapper(HttpContext httpContext) : base(httpContext) | |
{ | |
this.httpContext = httpContext; | |
} | |
public override HttpRequestBase Request | |
{ | |
get { return new AppHarborRequestWrapper(httpContext.Request); } | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web; | |
namespace NetflixDemo | |
{ | |
class AppHarborRequestWrapper : HttpRequestWrapper | |
{ | |
private readonly HttpRequest httpRequest; | |
public AppHarborRequestWrapper(HttpRequest httpRequest) : base(httpRequest) | |
{ | |
this.httpRequest = httpRequest; | |
} | |
public override Uri Url | |
{ | |
get | |
{ | |
var builder = new UriBuilder | |
{ | |
Scheme = httpRequest.Url.Scheme, | |
Host = httpRequest.Url.Host, | |
Port = 80, | |
Path = httpRequest.Url.AbsolutePath, | |
Fragment = httpRequest.Url.Fragment, | |
Query = httpRequest.Url.Query.Replace("?", "") | |
}; | |
var forwardedProtocol = httpRequest.Headers["X-Forwarded-Proto"]; | |
if (!String.IsNullOrEmpty(forwardedProtocol)) | |
builder.Scheme = forwardedProtocol; | |
if (builder.Scheme == "https") | |
builder.Port = 443; | |
if (httpRequest.IsLocal) | |
builder.Port = httpRequest.Url.Port; | |
return builder.Uri; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
using System.Web.Routing; | |
namespace NetflixDemo.Controllers | |
{ | |
public class HomeController : Controller | |
{ | |
public ActionResult Index() | |
{ | |
return View(); | |
} | |
public ActionResult About() | |
{ | |
return View(); | |
} | |
protected override void Initialize(RequestContext requestContext) | |
{ | |
base.Initialize(new RequestContext(new AppHarborHttpContextWrapper(System.Web.HttpContext.Current), requestContext.RouteData)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment