在 ASP.MVC 应用程序中默认路由到项目 URL

Default routing to Project URL in an ASP.MVC Application

我正在本地 PC 上测试 SSL 加密,这需要我将 ASP.MVC Project URLhttp://localhost:xxxx 更改为 http://payroll.net:81 之类的东西。 当我 运行 这个应用程序时, ASP.MVC 可能没有加载这个 URL 上的项目。浏览器出现以下错误:

The server at payroll.net can't be found, because the DNS lookup failed. DNS is the network service that translates a website's name to its Internet address. This error is most often caused by having no connection to the Internet or a misconfigured network. It can also be caused by an unresponsive DNS server or a firewall preventing Google Chrome from accessing the network.

我必须提到我已经将 ../WINDOWS/System32/drivers/etc/ 位置的 hosts 文件更新为:

127.0.0.1 payroll.net:81。

我还必须提到,如果我 运行 默认 Project URLIISExpress 使用的相同项目,即 http://localhost:xxxx,项目 运行s都好。关于这个项目如何在 IISExpress 上 运行 与 http://payroll.net:81 URL 有什么想法吗?

如果您想 运行 您的 Web 应用程序使用 https,只需将属性“[RequireHttps]”添加到您的控制器。

如果您有两个名为 HomeController.csActionController.cs 的控制器,则将此属性添加到两个控制器,例如

[RequireHttps]
public class HomeController : BaseController
{
// Your controller
}

因此,当您调用 HomeController 中的操作时,它会强制浏览器使用 https.

在@AbdulRaufMujahid 评论的帮助下,我设法解决了这个问题,并将回答我自己的问题。

C:\WINDOWS\System32\driver\etc\ 目录中的

hosts 文件不会将 IP 地址解析为 hostname 如果 port 使用 IP 地址指定.就我而言:

127.0.0.1 payroll.net:81 ------> 错误 ----- > 指定端口号 81 防止127.0.0.1(IP) 解析为 payroll.net(hostname).

我一删除 81 作为:

127.0.0.1 payroll.net ------> 正确

项目开始 运行 很顺利。 :-) 谢谢@AbdulRaufMujahid 和所有提供帮助的人。

如果使用 IP 地址指定端口,hosts 文件无法将 IP 地址解析为主机名。所以

127.0.0.1 payroll.net:81错了

127.0.0.1 payroll.net 正确

现在您可以安全地打开 https://payroll.net:81. (which now automatically maps to https://localhost:81) 只需确保站点托管在 IISExpress 的端口 81 上并且启用了 ssl

补充说明:

我想在第一条评论中说明的一点是,SSL 测试不需要您将 url 从 http://localhost:xxxx to https://payroll.net:81 (As mentioned in your question). For SSL testing. you can just host your site to any different unused port without changing hosts file e.g https://localhost:91

更改为

来源:

我在热汗汗回答中的评论和验证