将端口号分配给localhost

问题描述 投票:-1回答:2

我的应用程序中有一个URL http://localhost/Login.aspx无效:

<a href='http://localhost/Login.aspx'>Login</a>

但它适用于:

<a href='http://localhost:51807/Login.aspx'>Login</a>

问题是:

当用户点击登录51807时,如何将端口号http://localhost/Login.aspx分配给URL <a href='http://localhost/Login.aspx'>Login</a>

c# html asp.net localhost
2个回答
1
投票

使用相对链接

<a href='./Login.aspx'>Login</a>

1
投票

端口号由visual studio在项目创建期间生成,以避免在同时开发/调试多个Web应用程序时发生端口冲突。此临时端口不会影响您在生产中的部署

关于你的问题,考虑使用<asp:HyperLink>生成链接而不是使用普通的html <a>标签,例如:

<asp:HyperLink runat="server" NavigateUrl="~/Login.aspx">Login</asp:HyperLink>

ASP.NET会将url中的~转换为Web应用程序的实际根路径,确保链接始终相对于根路径。

例如在你的情况下http://localhost:51807/Login.aspx

或者在生产中生产http://some.domain/Login.aspx

或者如果您的Web应用程序部署为虚拟应用程序http://some.domain/virtualapp/Login.aspx

© www.soinside.com 2019 - 2024. All rights reserved.