开发者

Url.Action not including port number

开发者 https://www.devze.com 2023-03-20 17:09 出处:网络
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:

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.

开发者_StackOverflow中文版

Thanks in advance!


You could include it like this:

@Url.Action(
    "authenticate", 
    "auth", 
    null, 
    "https", 
    Request.Url.Host + ":" + Request.Url.Port
)

Of course this means that your local web server must support SSL which is not the case with Cassini. You could use IIS Express for this to work.


The integrated development server does not support HTTPS, but you can use your local IIS - see HERE.


Http and https connections are usually on two different ports so if you change from one protocol to the other you either need to use the standard port or supply a specific port yourself. There's no sensible way a generic system can determine the right port


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号