How to host ASP.NET Core 3.x MVC application on server

Sheonarayan
Posted by in .NET Core category on for Intermediate level | Points: 250 | Views : 13016 red flag

In the previous article, we learnt how to publish .NET Core 3.x MVC application from Visual Studio 2019. In this article, we will learn how to host ASP.NET Core 3.x MVC application on the server.

Introduction

Hosting ASP.NET Core 3.x MVC application is little tricky particularly when the .NET Core version is changing frequently and a lot of enhancements are being made for good. In this article, I will also address frequently faced issue while hosting ASP.NET Core 3.x application.

In case you have created ASP.NET Core 3.x API, the procedure of hosting is same as explained here.

Installing ASP.NET Core runtime

Before you proceed further, please ensure that your server has latest version of .NET Core (ASP.NET Core Runtime) installed. To verify if your latest version is installed or not. The easiest way to know is to open IIS on your server. Go to Default Web Site node in the left and click on Modules as displayed in the picture below.



This should open Modules installed in your server as displayed below. Now notice AspNetCoreModuleV2 module as selected in below picture. If its there, probability is that your .NET Core is installed. However this might be .NET Core 2.x so go to the next step and download the latest version of the .NET Core runtime.



To check which of ASP.NET Core runtime is installed, open your command prompt and write dotnet --version command as shown in the picture below and you will get the version number as displayed in the picture below.



If you do not have latest (version 3.1.2 at the time of writing this article), go to .NET Core download url from Microsoft website as displayed in the picture below.

Go to Run apps - Runtime (3rd column) and OS as Windows and click on Hosting Bundle link. Hosting Bundle ensures that you have all that is required to run the ASP.NET Core application on your server.



Now create a website in your IIS and go to its Application Pool. Select .NET CLR version as No Managed Codeand  click OK.


Now target your website to the Publish folder that was created after Publishing your website. Read How to publish .NET Core 3.x MVC application from Visual Studio 2019 here.

Though Publish creates many other folder and files however if you have installed the Hosting Bundle as suggested above, you only need Publish folder in order to run your application.

When you first time browse your application, you may end up getting following error.
HTTP Error 500.38 - ANCM Application DLL Not Found


To solve this error, you will need to change your web.config file of Publish folder to following. Notice the aspNetCore tag. Probability is your hostingModel attribute might be inprocess, change it to OutOfProcess.

Other few attributes that are very handy are following
  • stdoutLogFile - path to create log file if stdoutLogEnabled attribute is set to true.
  • stdoutLogEnabled - if true, this enable log file to be created under Publish/logs folder as specified in stdoutLogFile attribute.
  • Ensure that you have ASPNETCORE_ENVIRONMENT variable set under environmentVariables tag. This helps to set the hosting mode of the application. If value is Development, you get all the benefits of Development environment like Development specific logs, appSettings values etc.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
	  <aspNetCore processPath=".\myFunda.exe" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="OutOfProcess">
		  <environmentVariables>
			<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
		  </environmentVariables>
	  </aspNetCore>	 
	  
    </system.webServer>
  </location>
</configuration>

Also, you get all settings enabled that you specify in Configure method of Startup.cs as shown below.



In ideal scenario, your value of  ASPNETCORE_ENVIRONMENT variable should be "Production". By now we are almost done with all necessary settings and configuration that needs to be set to host ASP.NET Core 3.x application.

One very important thing to remember is to change the ConnectionStrings value of your appSettings.json file to ensure that your application is pointing to the right database.

In case you are getting any error or your application is not working, please go to Publish/logs folder and read the .log file content. If your hosting environment is Development,  and logging is enabled you should be getting all your error logged there in the .log file. Take necessary action as suggested in the .log file and you should be good to go.

IMPORTANT: One very important point to note is that once you are done with debugging issues of your application on your server, please ensure that stdoutLogEnabled attribute of aspnetCore tag is set to false in web.config file otherwise you will get huge .log file created that consumes your disk space of your server.
 
Hope you liked this article, thanks for reading. Please feel free to ask any question or issue you are facing while hosting .NET Core 3.x application on the server and I would try to answer.

Page copy protected against web site content infringement by Copyscape

About the Author

Sheonarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com

Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)