Introduction
Let’s build a .NET Core Web App in Azure! We will be performing all operations using the Azure CLI, and all of our work will be built using Visual Studio Code on Bash On Windows, Linux, Mac OS, or a container (we’ll containerize our app in a few weeks).
Prerequisites
- [Required] Azure CLI (install guide).
- [Required] .NET Core (1.0.0-rc4-004800).
- [Required] Azure Subscription.
- [Required] git.
- Visual Studio Code.
- Bash On Windows.
Build
NOTE: all command statements with multiple lines ignore the need for a newline escape.
Create a .NET Core Web App
Create the app
First, we are going to create a new .NET Web App. I am going to make a simple “Hello, World!” app. To your favorite shell!
1 | mkdir netcore_hello_world |
Next, we need to test that our app works locally.
1 | dotnet restore |
At this point, your shell will block, and you can test your app by navigating to the URL specified (in my case, it is http://localhost:5000/). Navigate to the simple web api endpoint that is provided by default (i.e., http://localhost:5000/api/values), and you should see the expected response!
1 | ["value1","value2"] |
Add a Web API endpoint
I have always wanted to have a website respond to my name, so I am going to add a Web API enpoint to my app which will respond the way I want. You can add any endpoint you would like here, so have it respond in Klingon: it’s your app, do what you want.
Awesomely enough, ASP.NET Core web apps have Web API routing built in. In the Controllers
directory, all I need do is simply emulate ValuesController.cs
to some degree. My new controller is going to be pretty simple since I just want to say “hello”. So, I simply create a new controller (Controllers/HelloController.cs
), and I add my controller class to it (usings and namespace ommitted).
1 | public class HelloController : Controller |
You will notice a few key points here:
[HttpGet]
informs the runtime that this method represents an HTTP GET endpoint.[Route("api/hello/{name}")]
informs the runtime that the endpoint will be located atapi/hello
, and it takes a parameter,{name}
. Thisname
parameter is reflected in the method signature ofSayHello
.- By convention, we are returning an
IActionResult
, which allows us to easily send status codes with our content (e.g.,this.Forbid
,this.Ok
).
Next, let’s try our change. First, Ctrl+C
to stop the local web server, and restart the server.
1 | dotnet run |
Navigate to the endpoint, and include your name (e.g., http://localhost:5000/api/hello/Aaron). You should see the expected response.
1 | { "response" : "Hello, Aaron‽" } |
If “Drumpf” tries, we will get the expected error result (i.e., Drumpf?...Really?
, with a 400
status code).
Publish to Azure
Configure Azure CLI
1 | az login |
Create a new web app on Azure
Using the Azure CLI, we can easily create a new web app instance on Azure.
1 | # Create a demo group for cleanup. |
Now, we can verify the host name of our newly created app.
1 | $ az appservice web show -g DemoGroup -n AaronDemoHelloApp --query hostNames --out tsv |
Navigating to this address (i.e., http://aarondemohelloapp.azurewebsites.net/) yields a default Azure web app screen.
Deploy to Azure via git
At the moment, we are just eager to get our app up on Azure. For now, we will configure deployment using local git.
1 | $ az appservice web source-control config-local-git -g DemoGroup -n AaronDemoHelloApp --out tsv |
However, we need to create a username and password for this deployment endpoint. Let’s set our deployment credentials through the Azure CLI.
1 | az appservice web deployment user set --user-name twitchax |
You will be prompted to set a password, and that’s it for deployment authentication!
Now, we can push our app straight from our shell with git.
1 | # Create a repository and make the first commit. |
Revel in your awesomeness
Navigate to your azure domain address (i.e., http://aarondemohelloapp.azurewebsites.net/api/hello/Aaron) to test your deployment. It’s alive!
At this point, we can share our domain name with the world! However, if we would like, we can fairly easily buy a custom domain and point that new domain (or a subdomain) at our new web app.
Obtain a domain name
Go buy a domain name. I mean, why not? Most of them are as low as $12 / year. I, personally, use Google Domains, but you can use your favorite service.
Configure a domain name
Configure CNAME with your registrar
I added a CNAME record for helloapp.twitchax.com
to aarondemohelloapp.azurewebsites.net
. In Google Domains, you just find your domain and click “DNS”. Then, add a “custom resource record” which points to your web app.
Bind the hostname in Azure
Azure requires that we specify which custom domains are allowed to point to our web app. So, back to the trusty Azure CLI, and we can bind to our custom domain name with one, simple command.
1 | az appservice web config hostname add -g DemoGroup --webapp AaronDemoHelloApp |
NOTE: There is currently a bug (#1984) in Azure CLI which prevents adding a host name, but a proposed fix is on the way! This same operation can be completed in the Azure Portal for the time being (Settings >> Custom domains).
Done
That’s it! In about 10 minutes, we have built our web app and pushed it to Azure!
Next week, we will add some data to our web app, so stay tuned.