简体   繁体   English

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

[英]launchSettings.json launchUrl doesn't work "api/values"

I am trying to change http://localhost:5001/api/values route but the program is stuck this url.我正在尝试更改http://localhost:5001/api/values路由,但程序卡住了这个 url。

I read this solutions我读了这个解决方案

How to change the default controller and action in ASP.NET Core API? 如何更改 ASP.NET Core API 中的默认控制器和操作?

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

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

Everyone write same thing but not work for me.每个人都写同样的东西,但不适合我。

My launchSetting.json file is我的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"
      }
    }
  }
}

I tried to change app.UseMvc();我试图改变app.UseMvc();

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

this is also not working.Where does api/values come from?这也不起作用api/values来自哪里? I can't figure out .我想不通。

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

When you create new ASP.Net Core Web API project you will see that in project property there is Launch browser setting that set to api/values path.当您创建新的 ASP.Net Core Web API 项目时,您将看到在项目属性中有设置为api/values路径的Launch browser设置。 So you can change it to what ever url you want or you can change in your launchSetting.json file因此,您可以将其更改为您想要的任何网址,或者您可以在 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"
      }
    }
  }
}

So you will see in profiles section there will be 2 config.所以你会在配置文件部分看到有 2 个配置。 One is for IIS express (when using Visual Studio to run your code) and WebApplication4 (when you run project using dotnet run) so you can change into一种用于 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"
      }
    }
  }
}

So when you use VS to run the project or dotnet run command is will always serve the swagger url first.因此,当您使用 VS 运行项目或dotnet run命令时,将始终首先提供 swagger url。

I am using VSCode and is happening the same to me, but on Windows.我正在使用 VSCode 并且在我身上发生了同样的事情,但是在 Windows 上。 I am just enhancing a bit the answer here.我只是在这里加强一点答案。

I have tried VS2019 and it worked in that IDE, so it is supposely to do with VSCode.我已经尝试过 VS2019 并且它在那个 IDE 中工作,所以它应该与 VSCode 有关。 I have searched for other answers and I found this.我已经搜索了其他答案,我找到了这个。

vscode launch.json debug and open specific url vscode launch.json 调试并打开特定的url

What fixed my issue was to go to .vscode/launch.json file and append:解决我的问题的是转到 .vscode/launch.json 文件并附加:

         "launchBrowser": {
            "enabled": true,
            "args": "${auto-detect-url}",
            "windows": {
                "command": "cmd.exe",
                "args": "/C start ${auto-detect-url}/swagger"
            }

as Anton Dremin describes, just add the above into the configurations section.正如 Anton Dremin 所描述的,只需将上述内容添加到配置部分即可。

The problem is because of Visual Studio for Mac.问题是因为 Visual Studio for Mac。 I was able to get the correct url to chance Run With>Custom Configuration > Run Action --> Debug .Net Core Debugger我能够获得正确的 url 来Run With>Custom Configuration > Run Action --> Debug .Net Core Debugger

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 launchSettings.json 和 appSettings.json 是如何工作的? - how do launchSettings.json and appSettings.json work? 为什么 Azure 应用服务不使用 launchSettings.json 配置? - Why Azure App Service doesn;t use launchSettings.json config? 更改launchSettings.json中的端口会显示“无法访问此站点” - Changing port in launchSettings.json gives “This site can’t be reached” Visual Studio for Mac 忽略launchSettings.json - Visual Studio for Mac Ignoring launchSettings.json 微服务未在 LaunchSettings.JSON 设置的端口上启动 - Microservice not Launching on Port Set by LaunchSettings.JSON Launchsettings.json 不是使用 dotnet new 创建的 - Launchsettings.json not created using dotnet new launchSettings.json 文件打开错误的 URL - launchSettings.json file opens wrong URL 从 Visual Studio 2019 在 Kestrel 下运行 .NET 5 Api 会忽略 launchSettings.json applicationUrl 设置 - Running .NET 5 Api under Kestrel from Visual Studio 2019 ignores launchSettings.json applicationUrl setting 如何在 linux 上的 launchsettings.json 中引用环境变量 - How to reference environment variables in launchsettings.json on linux 是否可以在 launchsettings.json 中将 executablePath 指定为相对路径? - Is it possible to specify executablePath as a relative path in launchsettings.json?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM