How to Hos MVC solution under SharePoint in IIS?
- 1.1 Prerequisites of Host MVC Application under SharePoint IIS
Steps to host an MVC application under SharePoint IIS
- 1.2.1 Step 1: Prepare the MVC
- 1.2.2 Step 2: Configure SharePoint IIS
- 1.2.3 Step 3: Review the .Net Target Version
- 1.2.4 Step 4: Test the MVC Web Application
In certain scenarios, you might need to host several MVC applications as subdirectories under the main domain of a SharePoint web application, without having to open additional ports or create additional subdomains.
In fact, the IIS integration allows you to host multiple applications under the same domain name and port, without without needing to set up distinct subdomains or open extra ports. However, this might pose more challenges when dealing with an existing SharePoint web application.
In this post, we will walk you through the detailed steps to learn more how we can host a new application under SharePoint web application in IIS.
To host an MVC application under SharePoint IIS, the following prerequisites must be met:
- IIS must be installed and configured.
- A SharePoint environment already set up.
- Have access to the SharePoint server and permissions to manage IIS.
- The MVC package is ready.
- The DB Server is ready and the new DB is restored.
To host an MVC application under SharePoint IIS, you have to do the following:
- Prepare the MVC Package.
- Configure IIS.
- Create a new Application Pool.
- Create a new Application under SharePoint Site.
- Adjust Permissions.
- Break parent Web Config settings.
- Review the web config of the new web application.
- Test the new application.
- Deploy the new application on all SharePoint Servers.
Step 1: Prepare the MVC
Before deploying your solution to the SharePoint server, you need first to complete the following steps:
- Ensure that your solution works as expected independently of SharePoint.
- Publish your MVC application to a folder on your local machine.
- Copy the contents of the published MVC application folder to the SharePoint server.
- Place the published MVC application folder in a directory accessible by the SharePoint application pool account.
After preparing the MVC solution package, you must configure the IIS settings on all SharePoint servers in your farm by following these steps:
- Create a new Application Pool.
- Create a new Application under SharePoint Site
- Adjust Permissions
- Break inheritance of Web Config settings
1) Create a new Application Pool
To create a new web application Pool, you have to do the following:
- Login to the SharePoint Server.
- Open IIS.
- Right Click on “Application Pools“, and Select “Add Application Pool“.
- Provide a name for the application pool that suits your preference, and leave the default adjustments as described below.
2) Change the Application Pool Identity
By default, the application pool identity for the newly created application pool is set to “ApplicationPoolIdentity.”
However, to ensure that your MVC solution functions properly under the SharePoint Web Application in IIS, I recommend using the same Application Pool Account that is currently used for the parent site.
To change the Application Pool identity, you have to do the following:
- Right click on the newly created Application Pool > select “Advanced Settings“.
- Scroll down to “Identity“, and click on the three dot button as stated below.
- Provide the same credentials of the Parent Application Pool Identity Account.
Now the application pool has been created and is ready for use, the next step is to create a new Application under SharePoint Web Site in IIS by doing the following:
- Open IIS > Expand “Sites“.
- Right Click on your SharePoint Web Site, and select “Add Application“.
- A new dialog will open where you need to provide the following:
- Alias: Name of your Application without any space.
- Application Pool: Select the newly created Application Pool.
- Physical Path: Set the physical path to the directory where you copied the MVC application.
- Click on “Test Settings” to ensure that the Application Pool Identity account has the permissions required to connect to the specified path.
- Then Click OK.
4) Adjust Permissions
If you encounter a permission issue when testing the connection in the previous step, it indicates that you need to adjust the permissions for your application folder.
You should grant the Application Pool’s Identity the necessary permissions on the folder that hosts the MVC package.
To adjust the permissions for the application folder, you have to do the following:
- Right click on the newly created application.
- Select “Edit Permissions“.
- Click on the “Security” tab, and provide the the account that you have set in the “ApplicationPoolIdentity” property, a “Modify” permissions as stated below.
5) Break inheritance of Web Config settings
This step is crucial, and it needs to be done to ensure that your MVC application functions as expected, without inheriting the Web.Config configuration from the parent SharePoint Web.Config file.
To break the inheritance for the web config from the parent site, you have to do the following:
- Open “CMD” as Administrator.
- Type “CD inetsrv” to navigate to the Internet Information Service Folder.
- Enter the following cmdlet, replacing “YourApplicationPoolName” with the actual name of your application pool. This will enable your MVC application to read the web config settings from its own configuration file, rather than inheriting them from the parent configuration
appcmd set config -section:system.applicationHost/applicationPools/[name='YourApplicationPoolName'].enableConfigurationOverride:"False" /commit:apphost
Step 3: Review the .Net Target Version
The final step is to review the Web Config File entries for the newly created application such as:
- Target Framework Number
- Custom Keys
- Connection String
In case the current SharePoint environment doesn’t have the same Target Framework Number , so you have to change it to “4.5” to get it working with current installed version.
In my case, the Target Framework is “4.8” which is supported by SharePoint 2019/2016 and already installed in the current SharePoint environment.
Don’t forget to grant the Application Pool Identity account an “Owner” permission for the Database on the DB Server, also make sure that the SharePoint Server can reach the Database Server as mentioned at How to Check SQL Server Connectivity between Application Server and Database Server
Step 4: Test the MVC Web Application
Before applying the same steps to other SharePoint Servers, it’s crucial to test your current application locally and address any issues that may arise, including:
- If you encounter issues with DLL version mismatches, make sure that all required DLLs are consistent and match the expected versions for your application to function correctly.
- Ensure that any custom keys in your Web Config are updated as needed to align with your specific application requirements.
- If the connection string is not updated. Please note that if it doesn’t work with Windows Authentication, you can try using SQL Authentication instead.
In conclusion, hosting an MVC solution under SharePoint in IIS involves several crucial steps:
- Create an Application Pool: Establish a dedicated application pool for your MVC application in IIS.
- Configure SharePoint Web Application: Create a new application under the SharePoint web site in IIS, specifying the relevant settings.
- Test Permissions: Ensure that the Application Pool Identity account has the necessary permissions to access the application’s folder.
- Separate Configuration: Adjust the MVC application’s settings and configuration to prevent it from inheriting settings from the parent SharePoint web.config.
- Check Target Framework: Confirm that the target framework of your MVC application aligns with the SharePoint environment. Modify it if necessary.
- Database Permissions: Grant the Application Pool Identity account “Owner” permissions for the database on the DB Server.
- Local Testing: Thoroughly test your application locally to identify and resolve any issues before applying the same steps to other SharePoint Servers.
- Consider Authentication Methods: If Windows Authentication doesn’t work, consider using SQL Authentication for database connectivity.
- Update Custom Keys: Ensure that any custom keys in your Web Config are updated correctly to match your application’s requirements.
- DLL Version Matching: Verify that DLL versions align with your application’s needs and address any version mismatches.
Hosting an MVC solution under SharePoint in IIS can be a complex process, but following these steps will help you successfully integrate your MVC application into the SharePoint environment.