【问题标题】:.net core 2.1 VS 2017 basic web api throwing "this site can't be reached".net core 2.1 VS 2017 basic web api 抛出“无法访问此站点”
【发布时间】:2020-06-06 09:27:48
【问题描述】:

我在.net core中按照WEB API的基本创建,运行项目时,我看到了

无法访问此站点连接已重置。 试试:

检查连接 检查代理和防火墙 运行 Windows 网络诊断程序 ERR_CONNECTION_RESET

我的 launchsettings.json 如下

 {
  "iisSettings": {
  "windowsAuthentication": false, 
  "anonymousAuthentication": true, 
  "iisExpress": {
  "applicationUrl": "http://localhost:14306",
  "sslPort": 44318
 }
 },
 "profiles": {
  "IIS Express": {
  "commandName": "IISExpress",
  "launchBrowser": true,
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
  },
   "InventoryService": {
   "commandName": "Project",
   "launchBrowser": true,
   "applicationUrl": "https://localhost:5001;http://localhost:5000",
   "environmentVariables": {
   "ASPNETCORE_ENVIRONMENT": "Development"
   }
  }
}
}

不确定在哪里可以检查代理设置,或者如果我遗漏了任何东西,我尝试了不同的解决方案,但都没有奏效。这是我第一次在 .net core 中创建项目。你能帮忙吗?

我在 Startup.cs 中添加了这段代码。

   public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseMvc();

    }

更新

我更新了端口号并安装了自签名证书,

在我的属性中我更新了端口

现在我看到了

找不到这个本地主机页面没有找到该网址的网页:https://localhost:5001/ HTTP 错误 404

【问题讨论】:

  • 我们需要更多信息。我假设您正在运行 Visual Studio? v2019?除了从模板创建项目并点击 Debug 之外,您是否做过任何其他事情?还是您更改/添加了代码?项目设置是否在 HTTPS 上运行?
  • 嗨 Bryan,感谢您的回复,我在 2017 年运行,我在哪里检查这个项目是否设置为在 https 上运行。在 Startup.cs 中,我确实添加了上面的代码。

标签: .net asp.net-mvc asp.net-core asp.net-web-api


【解决方案1】:

404 错误表示你的 web api 项目中没有这样的 Endpoint。

从您的 image1 中,launchUrl 未设置,因此当您启动项目时,它将显示 url https://localhost:5001/,由于您没有这样的端点,因此导致 404。要使其工作,请添加以下 api 控制器:

[Route("/")]
[ApiController]
public class ValuesController : ControllerBase
{
    // GET
    [HttpGet]
    public ActionResult<string> Get()
    {
        return "value";
    }
}

那么它应该可以工作了:

默认情况下,web api 模板会有一个默认的ValuesController(如果没有,请创建一个):

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    // GET api/values
    [HttpGet]
    public ActionResult<string> Get()
    {
        return "value";
    }
}

然后将您的 Launch 浏览器更改为 api/values

然后就可以了:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 2017-08-25
    相关资源
    最近更新 更多