I like using replit as my personal code editor mainly because of the convenience of an online editor. I thought it would be neat to show you how you can follow along with a tutorial from Microsoft to build a ASP.NET Core REST web site.
I am following the tutorial from here: https://learn.microsoft.com/en-gb/training/modules/build-web-api-aspnet-core/
First thing I did was log on to replit and then create a new C# repl called ContosoPizza.
This isn’t ready yet for running a web site as it only generates a console application but I can then make some changes to the structure of the repl to create a new ASP.NET project that will run the tutorial from the Microsoft learning web site.
Each repl will come with a console which will let you run most unix commands and if you cannot run a command the repl comes with a full unix shell as well to do some more complicated unix stuff. Personally I prefer not to jump into the unix shell unless I really need to and for what we are doing the console will provide enough functionality.
To create a dotnet project it is pretty simple, just go to the console and run this command: dotnet new webapi -f net6.0
This will setup a new web api project but if you click run on the repl you will get an error like the following:
Specify which project file to use because /home/runner/ContosoPizza contains more than one project file.
The reason for that is simple you have two project files in your root folder. To fix that delete the file main.csproj and the file main.cs. You can then run the repl but you cannot do a lot as the repl is running on ports that are not exposed outside of your repl virtual machine. Again this is simple matter to fix, simply open the file Properties/launchSettings.json and change the applicationUrl for ContosoPizza to “http://0.0.0.0”. When you click run you will see a blank screen, and you can now see the results at this url: https://contosopizza.muncey.repl.co/weatherForecast.
So to recap, what I have done is to create a new C# repl using http://replit.com called ContosoPizza. I then ran the dotnet sdk to generate a new web api project and removed the initial main.cs and main.csproj files so the project can run. Finally I changed the launch settings to use http://0.0.0.0 so that the project could be exposed via the replt web site.
This is the contents of my launchSettings.json
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:32264",
"sslPort": 44303
}
},
"profiles": {
"ContosoPizza": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://0.0.0.0",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Helpful links:
Leave a Reply