Url.Action not including port number

Viewed 4130

I have a partial view which is displayed on a non-HTTPS page, but POSTS to a controller that requires SSL. The form definition is this:

        <form id="authForm"
          method="post"
          action="@Url.Action("authenticate", "auth", new {}, "https")">

The problem I'm having is that, within Visual Studio and when debugging, the host and port are localhost:64043. However, the Url.Action call above doesn't put the port number in, meaning the browser directs to my IIS installation. Do I have to add something else, or override this method? I want my application to be location agnostic.

Thanks in advance!

4 Answers

Use Request.Host.ToUriComponent():

string actionUrl = Url.Action("Action", "Controller", new { blah = "" }, Request.Scheme, Request.Host.ToUriComponent());

This also correctly omits the port number when it is the default port for the scheme, i.e if the Scheme is HTTP and the port is 80, or the Scheme is HTTPS and the port is 443, it will be omitted.

Related