I'm building an ASP.NET, MVC5 site on Azure. My site has a mix of HTTP and HTTPS pages. I'd go to HTTPS-only, but I need to embed some 3rd party content (a twitch.tv stream) that is unsecured. Many browsers simply won't display it if it's served within an HTTPS page (without additional, not-always-intuitive user input). And going HTTP-only is not an option for stuff like login and account creation for, well, obvious reasons.
So I'm stuck straddling two worlds, which is fine, except for one issue. The problem I'm having is in emitting links with ActionLink
that bridge those two worlds (that is, a link to an HTTPS endpoint from an HTTP page, or vice versa). For these links, I'm forced to use the ActionLink
overload that takes a protocol parameter, and that variant seems to output the internal port number as part of the link.
For example if I have:
@Html.ActionLink("MySite", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
Then the final link is "http://localhost"
However, if I have:
@Html.ActionLink("MySite", "Index", "Home", "http", null, null, new { area = "" }, new { @class = "navbar-brand" })
Then the final link is "http://localhost:81". This URL yields a 404.
I did some digging into the MVC source code and it seems that ActionLink eventually calls into UrlHelper.GenerateUrl
, which eventually reads from requestContext.HttpContext.Request.Url
. This URL contains the Azure instance's internal port, which is dutifully added to the URL that it the function emits.
How are ActionLink
or GenerateUrl
supposed to know that the requestContext actually contains an inaccessible URL? Great question to which I don't have a good answer.
What I have concluded at this point, is that ActionLink
with the protocol overload is simply not appropriate for use in an Azure hosted solution. I've only reproduced this bug when I'm running it locally to test (i.e. when the load balancer and the web app are both running on the same machine, meaning they can't both use port 80). However, that's enough of a deal breaker for me that I can't proceed.
Is there a way to make ActionLink
work here? If not, what's the right way to generate links without it?