Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UrlHelper doesn't use referrer host when generating URIs

I am developing an MVC Web API application that I need to access from web browsers on many different devices, for example, smart phones. During development I want to be able to debug while I access the site from my phone. To do this, I have set up Fiddler to provide a reverse proxy. Locally, the server is running on localhost:55950, but from my phone I can access the site as MyComputer:8888. This part is working.

The issue is that I am creating URIs in my REST responses. When I access the site at MyComputer:8888, I need the URIs to be something like MyComputer:8888/services/api/files, but instead I get localhost:55950/services/api/files, which fails on my phone. I am using the UrlHelper class to generate the URIs. I've looked all over, but haven't found a way to tell the system to use the referrer, not the local host. I see the desired Referrer value in the Request, so I think I could write code to patch the URI, but it seems like this would be a common issue and that there must be a way to get UrlHelper to work correctly.

I'd greatly appreciate it if anyone could point me in the right direction.

like image 651
JSwartzen Avatar asked Nov 12 '22 01:11

JSwartzen


1 Answers

Check out this SO answer.

var httpContext = HttpContext.Current;

if (httpContext == null) {
  var request = new HttpRequest("/", "http://example.com", "");
  var response = new HttpResponse(new StringWriter());
  httpContext = new HttpContext(request, response);
}

var httpContextBase = new HttpContextWrapper(httpContext);
var routeData = new RouteData();
var requestContext = new RequestContext(httpContextBase, routeData);

return new UrlHelper(requestContext);
like image 146
Diego Avatar answered Nov 14 '22 21:11

Diego