简体   繁体   中英

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.

I read this solutions

How to change the default controller and action in ASP.NET Core API?

How to redirect root to swagger in Asp.Net Core 2.x?

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

{  "$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();

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? I can't figure out .

My controller attribute route is [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. So you can change it to what ever url you want or you can change in your launchSetting.json file

{
  "$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. 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

{
  "$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.

I am using VSCode and is happening the same to me, but on 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. I have searched for other answers and I found this.

vscode launch.json debug and open specific url

What fixed my issue was to go to .vscode/launch.json file and append:

         "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.

The problem is because of Visual Studio for Mac. I was able to get the correct url to chance Run With>Custom Configuration > Run Action --> Debug .Net Core Debugger

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM