Hi Guys,
I was able to successfully create a responsive and very fast website with EF6 and ASP.NET 4.5. Here is how i was able to do it successfully.
1. Create a basepage which all the webforms inherits and add the following code in the constructor of the basepage
a. you need to install a nuget package
PM> Install-Package EFInteractiveViews
b. Then add below code in your constructor of basepage class
public BasePage()
{
try
{
using (var ctx = new LPUOnlineEntities())
{
InteractiveViews.SetViewCacheFactory(ctx, new SqlServerViewCacheFactory
(ctx.Database.Connection.ConnectionString));
}
}
catch (Exception ex)
{
}
}
2. Enable ViewState Compression
a. Create two methods in your basepage class
protected override object LoadPageStateFromPersistenceMedium()
{
string viewState = Request.Form["__VSTATE"];
byte[] bytes = Convert.FromBase64String(viewState);
bytes = Compressor.Decompress(bytes);
LosFormatter formatter = new LosFormatter();
return formatter.Deserialize(Convert.ToBase64String(bytes));
}
protected override void SavePageStateToPersistenceMedium(object viewState)
{
LosFormatter formatter = new LosFormatter();
StringWriter writer = new StringWriter();
formatter.Serialize(writer, viewState);
string viewStateString = writer.ToString();
byte[] bytes = Convert.FromBase64String(viewStateString);
bytes = Compressor.Compress(bytes);
//ClientScript.RegisterHiddenField("__VSTATE", Convert.ToBase64String(bytes));
ScriptManager.RegisterHiddenField(this, "__VSTATE", Convert.ToBase64String(bytes));
}
b. A class which compresses the data
using System.IO;
using System.IO.Compression;
public static class Compressor
{
public static byte[] Compress(byte[] data)
{
MemoryStream output = new MemoryStream();
GZipStream gzip = new GZipStream(output,
CompressionMode.Compress, true);
gzip.Write(data, 0, data.Length);
gzip.Close();
return output.ToArray();
}
public static byte[] Decompress(byte[] data)
{
MemoryStream input = new MemoryStream();
input.Write(data, 0, data.Length);
input.Position = 0;
GZipStream gzip = new GZipStream(input,
CompressionMode.Decompress, true);
MemoryStream output = new MemoryStream();
byte[] buff = new byte[64];
int read = -1;
read = gzip.Read(buff, 0, buff.Length);
while (read > 0)
{
output.Write(buff, 0, read);
read = gzip.Read(buff, 0, buff.Length);
}
gzip.Close();
return output.ToArray();
}
}
3. Combine your scripts into 1
a. Give this nuget command
Install - Package Microsoft.AspNet.Web.Optimization
b. In application_strat in global.asax, Add the below code
System.Web.Optimization.BundleTable.Bundles.Add(new System.Web.Optimization.ScriptBundle("~/bundle/js")
.Include("~/Scripts/*.js"));
System.Web.Optimization.BundleTable.Bundles.Add(new System.Web.Optimization.ScriptBundle("~/bundle/css")
.Include("~/Styles/*.css"));
c. Remove all the references of javascript from the master page or web page and replace it with
<%: System.Web.Optimization.Scripts.Render("~/bundle/js") %>
<%: System.Web.Optimization.Styles.Render("~/bundle/css") %>
d. under system.web add the tag in web.config file
<system.web>
<compilation debug="false" />
</system.web>
4. Enable Compression on IIS from web.config under <system.webServer>.
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/javascript" enabled="true" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/javascript" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
</httpCompression>
5. To compress sessions add the following line in your web.config's <system.web> section
<sessionState compressionEnabled="true"></sessionState>
6. To compress your scripts add the following lines in your web.config
<system.web.extensions>
<scripting>
<scriptResourceHandler enableCompression="true" enableCaching="true" />
</scripting>
</system.web.extensions>
7. To reduce number of webresource.axd and scriptresource.axd request, Replace your ScriptManager with below code
<asp:ScriptManager ID="ScriptManager1" ScriptMode="Release" runat="server">
<CompositeScript>
<Scripts>
<asp:ScriptReference Name="MicrosoftAjax.js" />
<asp:ScriptReference Name="MicrosoftAjaxWebForms.js" />
</Scripts>
</CompositeScript>
</asp:ScriptManager>
I was able to successfully create a responsive and very fast website with EF6 and ASP.NET 4.5. Here is how i was able to do it successfully.
1. Create a basepage which all the webforms inherits and add the following code in the constructor of the basepage
a. you need to install a nuget package
PM> Install-Package EFInteractiveViews
b. Then add below code in your constructor of basepage class
public BasePage()
{
try
{
using (var ctx = new LPUOnlineEntities())
{
InteractiveViews.SetViewCacheFactory(ctx, new SqlServerViewCacheFactory
(ctx.Database.Connection.ConnectionString));
}
}
catch (Exception ex)
{
}
}
2. Enable ViewState Compression
a. Create two methods in your basepage class
protected override object LoadPageStateFromPersistenceMedium()
{
string viewState = Request.Form["__VSTATE"];
byte[] bytes = Convert.FromBase64String(viewState);
bytes = Compressor.Decompress(bytes);
LosFormatter formatter = new LosFormatter();
return formatter.Deserialize(Convert.ToBase64String(bytes));
}
protected override void SavePageStateToPersistenceMedium(object viewState)
{
LosFormatter formatter = new LosFormatter();
StringWriter writer = new StringWriter();
formatter.Serialize(writer, viewState);
string viewStateString = writer.ToString();
byte[] bytes = Convert.FromBase64String(viewStateString);
bytes = Compressor.Compress(bytes);
//ClientScript.RegisterHiddenField("__VSTATE", Convert.ToBase64String(bytes));
ScriptManager.RegisterHiddenField(this, "__VSTATE", Convert.ToBase64String(bytes));
}
b. A class which compresses the data
using System.IO;
using System.IO.Compression;
public static class Compressor
{
public static byte[] Compress(byte[] data)
{
MemoryStream output = new MemoryStream();
GZipStream gzip = new GZipStream(output,
CompressionMode.Compress, true);
gzip.Write(data, 0, data.Length);
gzip.Close();
return output.ToArray();
}
public static byte[] Decompress(byte[] data)
{
MemoryStream input = new MemoryStream();
input.Write(data, 0, data.Length);
input.Position = 0;
GZipStream gzip = new GZipStream(input,
CompressionMode.Decompress, true);
MemoryStream output = new MemoryStream();
byte[] buff = new byte[64];
int read = -1;
read = gzip.Read(buff, 0, buff.Length);
while (read > 0)
{
output.Write(buff, 0, read);
read = gzip.Read(buff, 0, buff.Length);
}
gzip.Close();
return output.ToArray();
}
}
3. Combine your scripts into 1
a. Give this nuget command
Install - Package Microsoft.AspNet.Web.Optimization
b. In application_strat in global.asax, Add the below code
System.Web.Optimization.BundleTable.Bundles.Add(new System.Web.Optimization.ScriptBundle("~/bundle/js")
.Include("~/Scripts/*.js"));
System.Web.Optimization.BundleTable.Bundles.Add(new System.Web.Optimization.ScriptBundle("~/bundle/css")
.Include("~/Styles/*.css"));
c. Remove all the references of javascript from the master page or web page and replace it with
<%: System.Web.Optimization.Scripts.Render("~/bundle/js") %>
<%: System.Web.Optimization.Styles.Render("~/bundle/css") %>
d. under system.web add the tag in web.config file
<system.web>
<compilation debug="false" />
</system.web>
4. Enable Compression on IIS from web.config under <system.webServer>.
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/javascript" enabled="true" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/javascript" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
</httpCompression>
5. To compress sessions add the following line in your web.config's <system.web> section
<sessionState compressionEnabled="true"></sessionState>
6. To compress your scripts add the following lines in your web.config
<system.web.extensions>
<scripting>
<scriptResourceHandler enableCompression="true" enableCaching="true" />
</scripting>
</system.web.extensions>
7. To reduce number of webresource.axd and scriptresource.axd request, Replace your ScriptManager with below code
<asp:ScriptManager ID="ScriptManager1" ScriptMode="Release" runat="server">
<CompositeScript>
<Scripts>
<asp:ScriptReference Name="MicrosoftAjax.js" />
<asp:ScriptReference Name="MicrosoftAjaxWebForms.js" />
</Scripts>
</CompositeScript>
</asp:ScriptManager>
No comments:
Post a Comment
Comments are welcome, Please join me on my Linked In account
http://in.linkedin.com/pub/ritesh-tandon/21/644/33b