Why isn't Request.Url.Scheme returning HTTPS?
This line:
var url = Url.Action("SomeHttpsMethod", "SomeHttpsController",
new { someParams }, Request.Url.Scheme);
constructs a URL based on your current request's scheme, which is HTTP. Request
always refers to the current request.
You'd be better off hard-coding "https" in this place since you always want it to be secure anyway:
var url = Url.Action("SomeHttpsMethod", "SomeHttpsController",
new { someParams }, "https");
Kevin Lee
Backend developer primarily working with C# ASP.NET Core and Angular. Interests include programming, anime, manga, badminton and piano.
Updated on May 17, 2020Comments
-
Kevin Lee almost 4 years
I am working on a local server and I need a specific URL to be accessed through HTTPS while the rest through HTTP. I have configured Visual Studio to use IIS Express so I can use HTTP/SSL.
I have a method like so:
[RequireHttps] public ActionResult SomeHttpsMethod() { //Do something }
In another place I have:
var url = Url.Action("SomeHttpsMethod", "SomeHttpsController", new { someParams }, Request.Url.Scheme);
If I access my site using
HTTP
i.e.http://localhost:httpport
, I still getHTTP
returned fromRequest.Url.Scheme
instead ofHTTPS
. Is that how it is meant to work?Obviously if I accesss my site using
HTTPS
i.e. to begin with i.e.https://localhost:sslport
,HTTPS
is returned (which is what I want) but I don't want to have to access the site inHTTPS
, only for that particular URL/controller method.