ASP.Net – How To’s
How to Deploy ASP.Net Core Code Changes
ASP.Net Core works differently from previous versions. Once a .Net Core application’s DLL has been loaded by the Windows runtime, that file is locked and cannot be overwritten while the application is running. In order to update the application, you must tell IIS to halt the application pool running it. Developers can do this by uploading a maintenance page named precisely “app_offline.htm” to the root of the site or root application folder, if the application is in a sub-folder of a site. The application pool will then shut down and the contents of “app_offline.htm” will be displayed in its place. Here is a walkthrough of the process to update code on each tier of your site:
Updating Code in Development
- Create an appropriate “app_offline.htm” if you haven’t already. It must contain something, but doesn’t need to be proper HTML. It can be as simple as “Application deployment in progress, please try back later.”
- Upload that file to the root of your site or application. For example, if your application has the URL www.purdue.edu/example/site/, upload app_offline.htm to \\wdvwebasp03.www.purdue.edu\webroot\www.purdue.edu\example\site\app_offline.htm
- (Optional) visit your Dev site and verify the app_offline.htm file is being displayed.
- Update the code in your Dev site.
- Delete the app_offline.htm file that’s in the root of your site. You may also opt to rename it, but know that if you do it will still be web-accessible.
- Test your updated application in Development.
Updating Code in QA
- Place your app_offline.htm file back in the root of your Development site.
- Deploy just app_offline.htm to QA using Deploy’s Advanced mode.
- (Optional) verify that your QA site is showing the contents of app_offline.htm.
- Delete app_offline.htm in Dev (or rename it, noting the caveat mentioned above).
- Deploy your whole site to QA.
- Test your updated application in QA.
Updating Code in Production
- Place your app_offline.htm file back in the root of your Development site.
- Deploy just app_offline.htm to QA using Deploy’s Advanced mode.
- Deploy just app_offline.htm to Prod using Deploy’s Advanced mode.
- (Optional) verify that your Prod site is showing the contents of app_offline.htm.
- Delete app_offline.htm in Dev (or rename it, noting the caveat mentioned above).
- Deploy your whole site to QA.
- Deploy your whole site to Prod.
- Validate that your Production site is back up and running your updated code.
Important: You only need to use app_offline.htm when updating your site’s application code. If you’re only updating CSS, images, JavaScript, or other static files not handled by the ASP.Net Core runtime you can upload and deploy those normally without taking your application offline.
How to implement CAS authentication in ASP.Net
CAS is the Central Authentication Service offered by the Identity and Access Management team within Purdue System Security (PSS). Unlike web server assisted authentication and authorization, it requires active application code in the web site to protect a resource.
Please visit the Purdue CAS Authentication page for how to get started.
How to configure my database connection to work in Dev, QA, and Prod
1. Make sure your web.config file has the following:
<configuration>
<connectionStrings>
<add name="dev" connectionString="server=devserver.itap.purdue.edu;database=mydb;uid=myuser;pwd=mydevpw" />
<add name="qa" connectionString="server=qaserver.itap.purdue.edu;database=mydb;uid=myuser;pwd=myqapw" />
<add name="prod" connectionString="server=prodserver.itap.purdue.edu;database=mydb;uid=myuser;pwd=myprodpw" />
</connectionStrings>
</configuration>
2. Make sure the code where you make your DB connection has something similar to the following code:
Please note, this was written in C#
SqlConnection conn = null;
string SQLServer = null;
// Figure out which tier this server is
string webServerTier = Environment.GetEnvironmentVariable("TIER");
try {
// Connection String to Database
String connString = ConfigurationManager.ConnectionStrings[webServerTier].ToString();
conn = new SqlConnection(connString);
// set up my SQL command
SqlCommand cmd = new SqlCommand("SELECT FName, LName FROM NAMES", conn);
conn.Open();
// SQL Reader to read the information obtained from the SQL command
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read() == true) {
String FName = (string)rdr["FName"];
String LName = (string)rdr["LName"];
// do something with the LName and FName variables, etc.
}
rdr.Close();
rdr.Dispose();
conn.Close();
conn.Dispose();
conn = null;
} catch (Exception ex) {
ErrorMessage.Text = ex.Message;
ErrorMessage.Visible = true;
}
How to publish an MVC application to the Shared ASP.Net cluster
The shared ASP.NET cluster does not have the MVC global reference libraries installed, so applications using MVC must include all dependencies. Visual Studio will automatically do this if you use it to publish your site:
- Make sure your development site (e.g. \\wdvwebasp03.www.purdue.edu\webroot\{sitename}) is mapped to a drive letter, for example Z:
- When ready to publish, right-click the top-level of your project in the solution explorer and select “Publish”
- If this is your first time publishing…
- Select the Custom option in the Publish wizard
- Give the new profile a descriptive name (your choice), such as “{project name} on Dev ASP.NET Server”
- Select “File System” as the Publish method
- For the target, select the root of your application’s path on the web server from your mapped drive. For example, given the mapping shown above, if Z:\ is mapped to \\wdvwebasp03.www.purdue.edu\webroot\{sitename}, and your application lives at {sitename}/{appname}, select Z:\{appname} here.
- Click the Publish button That’s it! Assuming other Visual Studio settings are at their defaults, your application will be published in the development sever with all the files it requires to run. If they’re not being published, right-click your project (like step 2 above), but select “Add Deployable Dependencies” and make sure all required dependencies are checked.