launchSettings.json launchUrl 不起作用“api/values”

Hak*_*çer 6 c# asp.net asp.net-core asp.net-core-webapi .net-core-2.2

我正在尝试更改http://localhost:5001/api/values路线,但程序卡在这个网址上。

我读过这个解决方案

如何更改 ASP.NET Core API 中的默认控制器和操作?

如何在 Asp.Net Core 2.x 中将 root 重定向到 swagger?

https://medium.com/quick-code/routing-in-asp-net-core-c433bff3f1a4

每个人都写同样的东西,但对我不起作用。

我的launchSetting.json文件是

{  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:54650",
      "sslPort": 44382
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "ShoppingBasketAPI": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我尝试改变app.UseMvc();

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-2.2

这也行不通。它从哪里来api/values?我想不通。

我的控制器属性路由是 [Route("api/[controller]/[action]")]

小智 6

当您创建新的 ASP.Net Core Web API 项目时,您将看到在项目属性中存在launchUrl设置为"api/values"路径的设置。因此,您可以将其更改为您想要的任何网址,也可以在 launchSetting.json 文件中更改

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:54356",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WebApplication4": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "applicationUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

所以你会在配置文件部分看到有 2 个配置。一种是针对 IIS Express(当使用 Visual Studio 运行代码时)和 WebApplication4(当您使用 dotnet run 运行项目时),因此您可以更改为

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:54356",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WebApplication4": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

因此,当您使用 VS 运行项目或dotnet run命令时,始终会首先提供 swagger url。


Hak*_*çer 0

该问题是由 Visual Studio for Mac 引起的。我有机会获得正确的网址Run With>Custom Configuration > Run Action --> Debug .Net Core Debugger