Setting up IIS for hosting an ASP .NET Core website without inetmgr in 3 easy steps!

In this post we are going to look at how to set up IIS to host a ASP .NET Core website without inetmgr, i.e., no GUI.

Our site will be called “www_mywebsite_com” which will be present in “C:\inetpub\wwwroot\mywebsite” and it will be listening on port 8080. In order to do this, we are going to be using a bat file and appcmd.

What is appcmd?

appcmd is a command line utility that was introduced by Microsoft when they released IIS 7 back in 2007. It can be used to modify or view any of IIS’s settings from the command line.

appcmd works best in command prompt, but you can use PowerShell’s “Start-Process” cmdlet to invoke appcmd. The syntax to invoke any appcmd functionality is:

APPCMD <COMMAND> <OBJECT> <ARGUMENTS>

Now that we know what appcmd is, let us now use it to set up IIS for an ASP .NET Core website.

Step 1: Adding an application pool

In order to add an application pool, we are going to make use of the “add” command on the “apppool” object as shown below:

C:\windows\system32\inetsrv\appcmd.exe add apppool /name:"MyWebsiteAppPool" /managedRuntimeVersion:""

In the above command we pass the name of the application pool using the “/name” switch and we set the CLR version to “No Managed Code” by passing an empty string to the “/managedRuntimeVersion” switch

Step 2: Adding a IIS website

To add the website, we are going to make use of the “add” command on the “site” object as shown.

C:\windows\system32\inetsrv\appcmd.exe add site /name:www_mywebsite_com /id:2 /physicalPath:C:\inetpub\wwwroot\mywebsite /bindings:http/*:8080:

We pass the name of our website (www_mywebsite_com) using the “/name” switch. We have to pass in a unique ID for our website. So, we pick ID 2, because site ID 1 will be taken by the “Default Website” in IIS. We pass the path in which our website will be present (C:\inetpub\wwwroot\mywebsite) using the “/physicalPath” switch. We pass our bindings, i.e., listen for http connections on port 8080, using the “/bindings” flag

Step 4: Link application pool to the website

In the last and final step we will be linking the application pool we created in step 1 to the website we created in step 2. In order to do this, we will use the “set” command on the “site” object as shown:

C:\windows\system32\inetsrv\appcmd.exe set site /site.name:"www_mywebsite_com" /[path='/'].applicationPool:"MyWebsiteAppPool"

In the above command, we select the website we want to modify using the “site.name” switch. We then use the “/[path=’/’]” switch to select the root application, i.e., ‘/’ and then combine that with the “applicationPool” selector to pass the name of the application pool we created earlier.

Now all that is left would be to copy the ASP .NET Core website artifacts to “C:\inetpub\wwwroot\mywebsite” and you can view the website at localhost:8080.

Summary

We created a new IIS website linked to an application pool without using inetmgr. Here are the commands we used:

C:\windows\system32\inetsrv\appcmd.exe add apppool /name:"MyWebsiteAppPool" /managedRuntimeVersion:""
C:\windows\system32\inetsrv\appcmd.exe add site /name:www_mywebsite_com /id:2 /physicalPath:C:\inetpub\wwwroot\mywebsite /bindings:http/*:8080:
C:\windows\system32\inetsrv\appcmd.exe set site /site.name:"www_mywebsite_com" /[path='/'].applicationPool:"MyWebsiteAppPool"

There is so much more that can be done with appcmd! To learn more, have a look at the official documentation here.

You can find this complete bat script here.

Share: Twitter Facebook
Lalit Adithya's Picture

About Lalit Adithya

Lalit is a coder, blogger, architect, and a photographer. He has been coding since 2010 and he has taken business critical websites and desktop apps from inception to production by working in/leading cross functional teams with an Agile focus. He currently focuses on developing & securing cloud native applications.

Bangalore, India https://lalitadithya.com