Minifycode 2021-10-03 Viewed 2.3K times ASP.NET MVC

In this article, you will learn how to Add Expires Headers on an ASP.NET MVC Website

  A web server uses the Expires header in the HTTP response to tell the client how long a component can be cached. Browsers use a cache to reduce the number and size of HTTP requests, making web pages load faster. 

YSlow to give me an A on the "Add Expires header" section by setting the web.config file.

So, if you want to use the Http expires headers for your static content, set it like this:-

Add this to your system.webserver web.config file

<staticContent>
  <clientCache cacheControlMode="UseExpires" httpExpires="Thu, 17 Sep 2020 00:00:00 UTC" />
</staticContent>

 

<system.webServer>
  <staticContent>
    <clientCache cacheControlMaxAge="10.00:00:00" cacheControlMode="UseMaxAge"/>
  </staticContent>
</system.webServer>

 

In the above example Expires are set for 10 days, 0 hours, 0 minutes, and 0 seconds.

 

Response Header
Cache-Control: private, max-age=259200
Content-Encoding: deflate
Content-Length: 3074
Content-Type: text/html; charset=utf-8
Date: Thu, 17 Sep 2020 03:17:43 GMT
ETag: 277f579d-5244-40b1-8
Expires: Sun, 20 Sep 2020 03:17:43 GMT
Last-Modified: Thu, 17 Sep 2020 03:17:43 GMT
SERVER
Vary: Accept-Encoding
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cache-Control: max-age=0
Connection: keep-alive
Cookie: _ga=GA1.2.255980350.1578940101; __gads=ID=d5c03ac2c33deef2-228f8ef4d8c20009:T=1597040533:RT=1597040533:S=ALNI_MYpWx_Bi2Nx4ULiXB2GZ45XD7B7HA; _gid=GA1.2.914256716.1600058412; ASP.NET_SessionId=a1cch5lxtihysvtclas3hakt; _gat_gtag_UA_153406186_1=1
Host: www.minifycode.in
If-Modified-Since: Wed, 16 Sep 2020 12:17:53 GMT
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36

How to Add Expires Headers on an ASP.NET MVC Website, What exactly are Expires Headers? Below is an explanation from Yahoo other Developer website: Proxies and browsers use a cache to reduce the number and size of HTTP requests, making web pages load faster. A web server uses the Expires header in the HTTP response to tell the client how long a component can be cached. So the files targeted to set expires headers on them are mostly the static If you use YSlow plugin as an assistant to help you improve your website performance, you probably saw that by default you get an F grade on “Add Expires headers” section.
public static byte[] CreateHTMLtoPDF(string HtmlString) { var htmlContent = String.Format(HtmlString); var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter(); htmlToPdf.Orientation = NReco.PdfGenerator.PageOrientation.Landscape; //var Orientation = new NReco.PdfGenerator.HtmlToPdfConverter().Orientation=NReco.PdfGenerator.PageOrientation.Landscape; htmlToPdf.Size = NReco.PdfGenerator.PageSize.A4; //htmlToPdf.Zoom = -0.55f; var pageMargin = new NReco.PdfGenerator.HtmlToPdfConverter().Margins; //htmlToPdf.PageHeight= 210; //htmlToPdf.PageWidth = 297; pageMargin.Left = 25; pageMargin.Right = 25; pageMargin.Bottom = 25; pageMargin.Top = 25; var pdfBytes = htmlToPdf.GeneratePdf(htmlContent); byte[] bytes; using (MemoryStream input = new MemoryStream(pdfBytes)) { using (MemoryStream output = new MemoryStream()) { string userpassword = "123";//Change it with User DOB Format dd-MM-yyyy string Ownerpassword = "123";//Change it with whatever owner is wanted PdfReader reader = new PdfReader(input); PdfEncryptor.Encrypt(reader, output, true, userpassword, Ownerpassword, PdfWriter.ALLOW_SCREENREADERS); bytes = output.ToArray(); //Response.ContentType = "application/pdf"; //Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf"); //Response.Cache.SetCacheability(HttpCacheability.NoCache); //Response.BinaryWrite(bytes); //Response.End(); } } return bytes; } <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendOTP.aspx.cs" Inherits="Shoping_Cart.SendOTP" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>How to Send and Verify OTP On Mobile No Using C# In Asp.Net</title> </head> <body> <form id="form1" runat="server"> <div> Mobile No: <asp:TextBox ID="txtMobileNo" runat="server" Width="200px"></asp:TextBox> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send OTP" /> <br /> <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label> </div> </form> </body> </html> using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Shoping_Cart { public partial class SendOTP : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { try { Random r = new Random(); string OTP = r.Next(1000, 9999).ToString(); //Send message string Username = "youremail@domain.com"; string APIKey = "YourHash"; string SenderName = "MyName"; string Number = "981111111"; string Message = "OTP code is - " + OTP; string URL = "http://api.minifycode.in/send/?username=" + Username + "&hash=" + APIKey + "&sender=" + SenderName + "&numbers=" + Number + "&message=" + Message; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL); HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); StreamReader sr = new StreamReader(resp.GetResponseStream()); string results = sr.ReadToEnd(); sr.Close(); Session["OTP"] = OTP; //Redirect for varification Response.Redirect("VerifyOTP.aspx"); } catch (Exception ex) { lblMessage.Text = ex.Message.ToString(); } } } } <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Verify.aspx.cs" Inherits="Cart.VerifyOTP" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Verify OTP</title> </head> <body> <form id="form1" runat="server"> <div> OTP: <asp:TextBox ID="txtOTP" runat="server" Width="200px"></asp:TextBox> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Verify OTP" /> <br /> <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label> </div> </form> </body> </html> using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Cart { public partial class OTP : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { if (Session["OTP"].ToString() == txtOTP.Text) { lblMessage.Text = "You have enter correct OTP."; Session["OTP"] = null; } else { lblMessage.Text = "Pleae enter correct OTP."; } } } }