Setup Launch Debug for the ASP.NET Core Project in VS Code

1. Introduction

VS Code is a great IDE for programmers. You can completely use it to develop .Net Core projects. It is lighter and faster than Visual Studio, and the powerful extensions can help you do almost everything you need!

By the way, if you are not Medium member, please click here to read.

You can use C# Dev Kit to easily develop the .Net project, it can provide you with intelligent code hints, and can provide the code auto-complete with AI

Do you want to be a good trading in cTrader?   >> TRY IT! <<

Ok, let’s get back to the point. If you want to debug the ASP.NET project in VS Code, you need to create a launch.json file, and the point is how can you setup a different environment in debug mode? and let VS Code auto launch the default browser for the page you want to debug?

2. Auto launch the debug page

By default, the launch.json file should look like below

Yes, if you just want to launch a .Net debug mode, that’s enough. But we want more!

For the above sample, this is a .Net Core API project, it’s using the swagger for API doc, and we want to auto launch the browser and redirect to the swagger index page, so we can add the below

"serverReadyAction": {
"action": "openExternally",
"pattern": "Now listening on:\\s+\"(http?://\\S+)\"",
"uriFormat": "%s/swagger/index.html"
},

serverReadyAction can define the action after launch the debug mode, and use the pattern to match the URL in console, and then redirect to the URL with uriFormat

For example, you will find the following in VS Code console after pressing F5 in debug mode:

3. Define the environment

For the business project, there should be a UAT and a Production version (or more versions), and most of them just have different configurations, so we can use below for setup the current debug environment

"env": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_URLS": "https://localhost:7041;http://localhost:5171"
},

The ASPNETCORE_URLS can be used for the launch debug page. After set the ASPNETCORE_ENVIRONMENT , it will use the difference appsettings.json based on this setting, suppose there are 3 appsettings.json files below

appsettings.json
appsettings.Development.json
appsettings.Production.json

The complete launch.json should be like below

4. Conclusion

I think VS Code can completely replace Visual Studio after .Net 6.0 for web development. The debug mode can be easily to fulfill most of situations, and I really love the intelligent code hints.

Loading

Views: 0
Total Views: 51