How to deploy an ASP.NET application in IIS?
In this page we will discuss about how to deploy ASP.NET web applications using IIS server, you might have bought a dedicated server or VPS server on Windows platform from HostingRaja.
Being one of the top managed hosting solution provider in India, Our technical team delivers the server to you with pre installed with all the required application. In case, If there is any issues in that, You can contact our technical team by contacting by raising a ticket. In case, If you want to install the IIS server by yourselves, you can follow the below methods or If your IIS server is already installed, Jump to the next section.
How to install IIS server on Windows Server - 2016
1. Once you login to you RDP, You will get a server manager screen as displayed below. If you don't have it, You can search for server manager windows. You can see the welcome Email to know about the RDP port number and credentials of the administrator.
2. In the above Dashboard Windows, Click on the Add Roles and Features as in the image above.
When you click on it, It takes you to the Add Roles and Features Wizard, It will ask for few verification or remainder for you to make sure that, Your Windows server is secure and configured correctly.
•The administrator account has a strong password.
•The network settings, such as IP addresses, are configured.
•The most current security updates from Windows® Update are installed.
3. Now you will be on the Before You Begin page, you have to click Next to continue the installation of IIS server.
4. You will be taken to Installation Type page and select Role-based or feature-based installation to configure a single server. Click Next.
5.In will take to you the server selection page, You have to choose the option Select a server from the server pool, and then select a server; Press on Next to continue the IIS server installation.
6. On the next page Server Roles page, select Web Server (IIS). From this page only, Most of the servers and services are installed on the Windows server. In Windows 2016, It is very easy to install all the server or services required. On the left hand side, You will see a description about all the services.
7.Add Features to install the IIS Management Console, and then click Continue. You can select the .NET framework and so many other features can be added just by clicking it. The following features can be added.
.NET Extensibility 4.5
.NET Extensibility 3.5 (If ASP.NET 3.5 was selected)
8.On the Server Roles page, select the features as shown in the below image, Once you are done, Make sure to verify it and then go to the next page by clicking Next.
You can continue to the installation by choosing the default values. In fact, Some customers might find it very difficult to understand this and thats why HostingRaja, Provides managed support. You can contact us anytime, we will be very happy to help you.
Make sure to verify the selected rol services and features are chosen correctly on the confirmation page. Restart the server, so that the changes are taken care immediately.
You can refer this Video tutorial to see the step by step guide https://www.youtube.com/watch?v=tXsVYJUUZ_s
By default IIS server runs on port 80, All the web browser by default connect to port 80. Once you restart the server, Open your browser, by default IE comes with server, and then in address bar type http://localhost to see your server running.
Install SQL Server Express
Basically, SQL Server Express will not be installed in the LocalDB because LoaclDB is not designed to work with IIS, so to test this you need to have SQL Server Express installed. But if you are using Visual Studio 2010 SQL Server Express then by default it will be installed. And if you are using Visual Studio 2012 then you have to install manually.
You will get this software online or visit the official website of Microsoft. Note: Choose correctly, or else your system will fail to install, but you can try installing another one.
Once after downloading you need to click on New SQL Server stand-alone installation or else, you can add features to an existing installation and follow the instructions by accepting the default choice.
Create SQL Server Express databases for the test environment
In this article we will take Contoso University as an example. And this university has two databased that is a membership database and application database. So now if you want to deploy these databases two separate databases or to a single database then you can easily do it by combining them between your application database and your membership database. And if you are planning to deploy to a third party hosting provider then your hosting plan should have that option.
So to deploy you need to go View menu in Visual Studio and next you need to choose Server Explorer and then you need to right click the Data Connection option. In here click on Create New SQL Server Database.
Now in here you need to add ".\SQLExpress" in the Server name box and "aspnet-ContosoUniversity" in New database name and then click on Ok.
You can follow the same procedure to create a new SQL Server Express databased "ContosoUniversity". So go to Server Explorer and in here it will show two databases.
Create a script for databases
On your computer when the application runs the IIS it will access the database by using the default application pool credentials. But these by default application pools will not have any sort of permission to open the databases. So to open the database you need to run a script where it will grant you the permission for that database. So now you need to create the script that you will run later to open te database while running in IIS.
So next you need to right click the solution and then click on Add New item and in here you need to create a new SQL File named Grant.sql. Once done use the below command into that file and then save and close it.
Running the script in the application database
In the mean time you need to configure the publish profile to run the above script in the membership database. Because during the deployment process database will use the dbDacFx provider. So during Code First Migrations deployment time you cannot run the command. Therefore you have to manually run the script before deployment in the application database.
1. Now go to Visual Studio and then open the Grant.sql which you have created
2. Next click on Connect
3. Now it will take you to Connect to Server over there you will see a dialog box, in there enter .\SQLExpress as the Server Name, and then click on Connect.
4. In here you will find the database drop-down list over there select the database ContosoUniversity, and then click on Execute
Publish to IIS
To publish to IIS there are many ways using Visual Studio and Web Deploy:
1.Use Visual Studio one-click publish.
2. Publish from the command line.
3.Create a deployment package and install it using the IIS Manager UI. The deployment package consists of a .zip file that contains all the files and metadata needed to install a site in IIS.
4. Create a deployment package and install it using the command line.
You need to make sure that you are running Visual Studio in administrator mode before publishing. And if you are not able to see Administrator option in the title bar then close the Visual Studio. And in Windows 8 Start page or in Windows 7 Start menu and then right click the icon to change the Visual Studio version and then select Run as Administrator. Because Administrator mode is required for publishing only when you are publishing to IIS on the local computer.
Create the publish profile
1. In Solution Explorer, you need to right click the ContosoUniversity and then click on Publish. Now the Publish Web wizard will appears.
2. Next in the drop-down list you need to select <New...>
3. Now in the New Profile dialog box you need to type as "Test" and then click on Ok. Now the wizard will automatically advance to the Connection tab.
4. Here in Service URL option you need to type the loachost
5. Next in the Site/application option you need to enter the Default Web Site/ContosoUniversity
6. In Destination URL option you need to enter http://localhost/ContosoUniversity. Over here Destination URL setting is not required. So when the Visual Studio finishes deploying the application, it will automatically open your default browser to this URL.
7. Next Click on Validate Connection to verify whether the settings are correct and you can connect to IIS on the local computer. Over there if you see a green check mark then your connection is successful.
8. Click Next to advance to the Settings tab.
9. Now you will see the Configuration drop-down option in here it will specify the build configuration to deploy.
10. Next you need to expand the File Publish Options and then click on Exclude files from the App_Data folder.
11. You can leave the Precompile during publishing and you can also remove additional files at destination check boxes cleared.
Configure deployment for the membership database
By following the below steps you can apply to the DefaultConnection database section of the dialog box.
1. You need to connect to the Remote connection string, over there you need to enter the bellow connection string where it helps to points to the new SQL Server Express membership database.
Data Source=.\SQLExpress;Initial Catalog=aspnet-ContosoUniversity;Integrated Security=True
Now this deployment process will put this connection string in the deployed Web.config file because Use this connection string at runtime is selected.
If you want you can also get the connection string from Server Explorer. In Server Explorer, expand Data Connections and select the <machinename>\sqlexpress.aspnet-ContosoUniversity database, then from the Properties window copy the Connection String value. That connection string will have one additional setting that you can delete: Pooling=False.
2. Next you need to select on Update database.
3. Now in here you need to click on Configure database updates.
4. In Configure Database Updates option you need to click on Add SQL Script and then you need to navigate that to the Grant.sql script that you saved previously.
5. You can follow the same process to add the aspnet-data-dev.sql script.
6. Once done Click on Close.
Configure deployment for the application database
So now when the Visual Studio detects an Entity Framework DbContext class, it will create an entry in it database section where it has Execute Code First Migrations option instead of an Update Database. And in some situations, you might be using a DbContext database but if you want to use the dbDacFx provider instead of Migrations to deploy the database. So in that case use it.
Follow the below steps to apply the SchoolContext database in the Databases section:
1. In Remote connection string option you need to enter the below connection string which will help you to points to the new SQL Server Express application database.
Data Source=.\SQLExpress;Initial Catalog=ContosoUniversity;Integrated Security=True
Now this deployment process will put this connection string in the deployed Web.config file because Use this connection string at runtime is selected. If you want you can use get the application database connection string from Server Explorer the same way you got the membership database connection string.
2. Next you need to click on Execute Code First Migrations. By clicking on this it will cause the deployment process to configure the deployed Web.config file to specify the MigrateDatabaseToLatestVersion initializer. And this will initializer automatically updates the database to the latest version when the application accesses the database for the first time after deployment.
Configure publish profile transforms
1. Click on Close and then click on Yes where it asks whether if you want to save changes.
2. Next in Solution Explorer you need to expand Properties, expand PublishProfiles
3. Up next right-click Test.pubxml, and then click Add Config Transform.
4. Next in the Web.Test.config transform file you need to insert the below command immediately once after opening the configuration tag.
<add key="Environment" value="Test" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
5. Once done click on Save and then close the file
6. Next you can right click the Web.Test.config file and you can click on to Preview Transform option where you can make sure that transform you coded produces the expected changes.
Preview the deployment updates
1. To preview the deployment of your updates you need to open your Publish Web wizard
2. Now in the Preview tab you need to make sure that the Test profile is still selected and then you need to click on Start Preview option to see the list of files that have been copied.
3. Once done click on Publish
Note: If your Visual Studio is not in administrator mode then you might get some error message where it shows error. So to over come that you can close the Visual Studio and then open it on administrator mode. But if you have are already in administrator mode, you will see a window showing the output.
Test in the test environment
Now you might notice that in one of your environment it might indicate like "Test" instead of "Dev" where it indicates that the Web.config transformation for the environment was successful. If you want to verify you can run the Instructors page and you can check the database. And over there when you select this page it may take few minutes to load because Code First creates the database and then runs the Seed method.
Over there you can click on the Students tab to verify that the deployed database has no students. Next you can select Add Students from the Students menu and add a student. Now you can view the new student in the Students page where you can cross verify that you have written successful database.
Next from Courses option, you need to select Update Credits menu. But here in Update Credits page it requires administrator permission, only then you will get the login page. Now in the login page you need to enter the administrator account credentials. Next Update Credits page will be displayed where it verifies that the administrator account that you have created previously was correctly deployed to the test environment.
You can also verify that an Elmah folder exists in the c:\inetpub\wwwroot\ContosoUniversity folder with only the placeholder file in it.
Review the automatic Web.config changes for Code First Migrations
To review the changes you need to open the Web.config file in your deployed application at C:\inetpub\wwwroot\ContosoUniversity and then you will be able to see where the deployment process configured Code First Migrations to automatically update the database to the latest version.
But that deployment process will also create a new connection string for Code First Migrations where it is exclusively used for updating the database schema
Having an additional connection string will enable you to define one user account for database schema updates and a different user account for the application of data access.
1. So in the Setting opting of the Publish Web wizard you need to enter the connection string where it specifies a user with its complete database schema permissions, and it also helps to clear the connection string at runtime checkbox. And in the deployed Web.config file, this becomes the DatabasePublish connection string.
2. Up next you need to create a Web.config file transformation for the connection string where you want your application to use at runtime.
Once after you deploy your application to IIS on your development computer and tested it there you will see the below result.