12

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://learn.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]")]

4 Answers 4

10

When you create new ASP.Net Core Web API project you will see that in project property there is launchUrl 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.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes but this isn't work both of them . I stil get api/values route.Can it be because I use macOS?
@HaktanEnesBiçer I dont think so . Try to search api/values in the project to see if there any code left contain that ? My approach work perfectly fine. I think there is something wrong in the code
I found the problem. It is because of visual studio mac.I searched before 'api/values' it can't find any code.
Wow, almost everything you wrote is wrong. There is no Launch browser setting, it's launchBrowser and the OP has the right value, VSC is not honoring it.
1

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.

Comments

0

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

Comments

0

The solution for me was modify launch.json...

{
    "version": "0.2.0",
    "configurations": [
        {
            [...]
            "serverReadyAction": {
                [...]
                // Workaround as using `"launchUrl": "swagger"` in "src/My/Project/Properties/launchSettings.json"
                // doesn't work.
                "uriFormat": "%s/swagger"
            },
            [...]
        }
    ]
}

Thanks! 😎

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.