MVC3 generates urls including port number, how to stop it
We just discovered a problem with our new cloud hosting. Basically NAT / port forward was set up because they do not allow multiple internal IP addresses so that SSL certificates can be executed for multiple sites.
This causes a problem with any MVC route that is generated where the protocol is also specified. Additionally, if you try to navigate to a URL that requires login, the ReturnUrl parameter in the redirected https URL also contains the port number.
The question is, is there a way to tell MVC to never include the port number, even if present, without using the UrlHelpers method / route overload, which allows you to specify the domain and therefore manually disable the port number.
If it's not obvious from what the request process is, here's a small example:
- Browser users make a request to the site
- Server forwards the same URL but with the port number attached
- Page created
- The response is sent back to the users' browser.
- The user logs in, but now the return url has a port, so when it redirects the user, the page cannot be rendered.
Due to malarkey port forwarding, this seems to lead to confusion in MVC routing. Not knowing the technical side of the server so well, I'm not sure if it's just a hosting company that isn't configuring something correctly in IIS or NAT-based, or just there is some web.config stuff that needs to be configured in order for MVC could generate correct urls as if this malarkey port does not exist.
Any advice or pointers are most appreciated.
source to share
Luckily for me, it turns out there is a configuration solution (because people asked, "How about changing the code?" → Me: "Seriously? NO, it must be a configuration issue").
The solution is simple to implement; add the following to the section <appSettings>
of your web.config file:
<add key="aspnet:UseHostHeaderForRequestUrl" value="true" />
This way, when ASP.NET/MVC generates URLs, it will always use the host header for the website the user requested from, instead of the port forwarding version, yay!
source to share