ASP.NET 4.5 and ASP.NET Core 1 Hosting BLOG

Tutorial and Articles about ASP.NET 4.0 and the latest ASP.NET 4.5 Hosting

ASP.NET 4.5 Hosting - Using Bundling and Minification in ASP.NET 4.5

clock April 25, 2013 09:12 by author andy_yo

Is it Important?
Minimising the number of requests the page has to perform can have a considerable effect on your site’s performance. IE6 and IE7 both limit the number of concurrent requests to 2, IE8 can handle up to 6. There is a lot you can to improve the initial load speed speed – one of which is bundling all your CSS and JS into two separate files. How much of a difference it could do. Well, as it turns out up to 30seconds on slower connections.

About ASPHostPortal.com

ASPHostPortal.com is Microsoft No #1 Recommended Windows and ASP.NET Spotlight Hosting Partner in United States. Microsoft presents this award to ASPHostPortal.com for ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2012, ASP.NET 4.5, ASP.NET MVC 4.0, Silverlight 5 and Visual Studio Lightswitch. Click here for more information

Bundled and Minified vs. Non-Bundled
This is the standard ASP.NET MVC4 app with all the initial JS libraries and CSS. Over a slower connection, the difference can be up to 30seconds. However, even on faster connections, you can save up two seconds just by combining and minifying your scripts.

Image 1 : Non Bundled

Image 2 : Bundled and Minified

Bundling and Minification in ASP.NET 4.5

Luckily for us, the ASP.NET now ships with a new library called System.Web.Optimization. It provides pluggable bundling and minification functionality for your scripts and styles.

It lets you define bundles at application start and pass them to the BundleCollection. Creating a basic new bundle is quite simple. Let’s assume we would like to combine few CSS files

protected void Application_Start()
{
    ... other startup logic
 
    var cssBundle = new StyleBundle("~/Content/themes/base/css")
            .Include("~/Content/themes/base/jquery.ui.core.css",
            "~/Content/themes/base/jquery.ui.resizable.css");   
    BundleTable.Bundles.Add(cssBundle);
}

You can also create bundles for your JavaScript.

protected void Application_Start()
{
    ... other startup logic
 
    var jsValidationBundle = new ScriptBundle("~/bundles/jqueryval")
              .Include("~/Scripts/jquery.unobtrusive*",
                        "~/Scripts/jquery.validate*"));
    BundleTable.Bundles.Add(jsValidationBundle);
}

Both StyleBundle and ScriptBundle take url of the bundled file as a constructor argument and use extension method .Include to add files. You can also use wildcard characters such as * in the include array. If you want to add the entire folder, use IncludeDirectory extension.

One thing to note, is what version of the System.Web.Optimization you have. The older version that came with the MVC4 beta used AddFile() syntax to add files to the bundles. However, if you install VS 2012RC you get a newer version of the DLL, which is a bit neater and uses the syntax shown above.

Rendering Helpers

The library also comes with two awesome helpers. When you develop locally, you want to have all the bundling setup, but you don’t want the bundling and minification to happen – it’s much easier to debug. The System.Web.Optimization has two helpers that address exactly that issue.

  <head>

        ... other content
        @Styles.Render("~/Content/themes/base/css", "~/Content/css")
        @Scripts.Render("~/bundles/jqueryval")
  </head>

When you run the debug setting in the compilation element in your web.config as false, the Styles and Scripts helpers will render the bundled files. However, settings debug=”true” will render them unbundled. Pretty cool!

<system.web>

    .....
    <compilation debug="false" targetFramework="4.5" />
    ....
</system.web>

And that’s not everything, the minified files will also have cache busting string attached based on the files in the bundles.

<link href="/Content/themes/base/css?v=UM624qf1uFt8dYtiIV9PCmYhsyeewBIwY4Ob0i8OdW81" rel="stylesheet" type="text/css" />



ASP.NET 4.5 Hosting - Security Improvement in ASP.NEt 4.5

clock March 18, 2013 08:21 by author andy_yo

The .NET 4.5 framework was released a couple of months ago and it included several improvements in the security area. To benefit from these improvements you need to do a few changes to you application's configuration file.
There are some important improvement in ASP.NET 4.5:

  • There are changes to the ASP.NET request validation, it now supports deferred (lazy) validation, as well as giving the option to fetch data unvalidated.
  • The AntiXSS library is included in the framework.
  • There are significant Cryptographic Improvements in ASP.NET 4.5.
  • Windows Identity Foundation is now included in the framework, referred to as WIF 4.5.

About ASPHostPortal.com

ASPHostPortal.com is Microsoft No #1 Recommended Windows and ASP.NET Spotlight Hosting Partner in United States. Microsoft presents this award to ASPHostPortal.com for ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2012, ASP.NET 4.5, ASP.NET MVC 4.0, Silverlight 5 and Visual Studio Lightswitch. Click here for more information

To take advantage of these new bits you'll have to do a bit of configuration, we'll get into that right away:

Switching to 4.5
While retargeting a couple of MVC applications to the new framework version, I learned that it's not enough to install the 4.5 framework and change the "Target framework" accordingly. You'll find that a comment appears in the web.config file:
<!--
    For a description of web.config changes for .NET 4.5 see http://go.microsoft.com/fwlink/?LinkId=235367.       
    The following attributes can be set on the <httpRuntime> tag.      
     <system.Web>
        <httpRuntime targetFramework="4.5" />
     </system.Web>

-->
It's important that you set the targetFramework in your configuration file, else your application will run in "4.0" mode.

Enabling AntiXss
You'd want to set the AntiXss library as the default encoder — that can easily be done in the httpRuntime configuration element:

<httpRuntime targetFramework="4.5" encoderType="System.Web.Security.AntiXss.AntiXssEncoder,System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

Note that there can be side effects to this, as AntiXSS takes a white list approach to encoding. That means that there may be characters that weren't encoded before, that will be encoded by AntiXSS.

Request validation
Lazy validation was introduced in ASP.NET 4.5, I just did some testing on it and it seems that lazy validation is the enabled regardless of how you set the "requestValidationMode", after you've installed the 4.5 framework. However, if you need access to any request parameters unvalidated, you'll need to set the validation mode to "4.5", as such:
<httpRuntime targetFramework="4.5" requestValidationMode="4.5" />
This will give you access to the unvalidated collections of parameters, e.g.:

Request.Unvalidated.QueryString["lastName"];
This is a much better approach than disabling request validation altogether. But use it with care, as always you should throroughly validate the input.

WIF 4.5
WIF is now part of the framework — that meant some breaking changes. It shouldn't take to much time to upgrade though,  particularly if you're concerned with RP's. There's a great article on MSDN with Guidelines for Migrating an Application Built Using WIF 3.5 to WIF 4.5.
There's two apparent changes I'd like to point out. First, you no longer need to set the "requestValidationMode" to "2.0" to cope with the request validation exceptions on the SignInResponseMessage's posted from an STS. WIF 4.5 plays nicely with the 4.5 request validation. Second, WIF now includes a MachineKeySessionSecurityTokenHandler which encrypts and MAC's WIF cookies based on the machine key. You'll find everything you need to set it up in: WIF and Web Farms.

 

 



ASP.NET Hosting - Resolve Errror "Validation of viewstate MAC failed"

clock March 5, 2013 08:57 by author andy_yo

Usually we won’t got “Validation of viewstate MAC failed” error in our site all the time. Normally the site works fine and doesn't throw any errors if you load the site and use it at a regular pace.  The error only occurs when you load the page, leave it for a while, and then continue working on the page without reloading or refreshing it.  I discovered this error because I opened the site, began using it, left for lunch, came back an hour later, and when I resumed using it and did anything that caused postback, it immediately threw the above error.  Unfortunately in order to re-create the error, I have to wait a while, which is very inconvenient for testing/troubleshooting. After a few hours of reasearch I found the solutions:

About ASPHostPortal.com

ASPHostPortal.com is Microsoft No #1 Recommended Windows and ASP.NET Spotlight Hosting Partner in United States. Microsoft presents this award to ASPHostPortal.com for ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2012, ASP.NET 4.5, ASP.NET MVC 4.0, Silverlight 5 and Visual Studio Lightswitch. Click here for more information

Error message:

Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.

Solution:

Yet another very teasing issue ASP .Net developers face. Solution is pretty much simple.

Add following link in your web.config file.

<pages enableEventValidation="false" viewStateEncryptionMode="Never" />

Above line should be included within section.

OR

if you don't want this solution to implemented on project level then you can add these attribute to the page(Top most line of HTML view) directive of the page which is creating issue.

That means you have to add enableEventValidation="false" viewStateEncryptionMode="Never" in page directive.

 



ASP.NET 4.5 Hosting - ASP.NET 4.5 Asynchronous pages

clock March 4, 2013 10:37 by author andy_yo

To make a page asynchronous in earlier versions of ASP.NET, the page has to implement the interface IHttpAsyncHandler and define concrete definitions to all the methods declared in the interface. This takes considerable amount of time and effort to make the page asynchronous.
In ASP.NET 4.5, we can turn the execution of any operation asynchronous by using async and await keywords. Any new page added to an ASP.NET application is considered synchronous by default. We can change this by setting value of Async property of the Page directive to true. Once this property is set, we can use async and await keywords in any method in the code behind file.

<span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; line-height: 115%; mso-ansi-language: EN-IN; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;"><%@ Page Language="C#" AutoEventWireup="true" Async="true" CodeBehind="Default.aspx.cs" Inherits="Async.Default" %>
</span>

About ASPHostPortal.com

ASPHostPortal.com is Microsoft No #1 Recommended Windows and ASP.NET Spotlight Hosting Partner in United States. Microsoft presents this award to ASPHostPortal.com for ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2012, ASP.NET 4.5, ASP.NET MVC 4.0, Silverlight 5 and Visual Studio Lightswitch. Click here for more information

This is because, performing asynchronous operations at wrong time may lead to some dangerous conditions on the server. By setting the Async attribute of the page, we are telling the server that, the current page is a safe place to perform async operations. Following is a sample page load event handler that calls a WCF service asynchronously and binds data to a GridView:

protected async void Page_Load(object sender, EventArgs e)
{
    var client = new DataServiceClient();
    var gettingCities = await client.GetCitiesAsync();
    gvCities.DataSource = gettingCities;
    gvCities.DataBind()                                               
}

Following are the set of steps performed when ASP.NET detects the await keyword in the above event handler:

  • Continues executing other synchronous tasks of the life cycle event
  • Once the above step is finished, the underlying synchronization context fires up an event saying that an async operation is pending
  • ASP.NET waits asynchronously till the pending task is completed and then it continues executing the rest of the statements
  • With above step, the life cycle (page load in this case) event is over. The control goes ahead to the next life cycle event

There is another way to achieve this. It is shown in the following snippet:

protected void Page_Load(object sender, EventArgs e)
{
    RegisterAsyncTask(new PageAsyncTask(async () =>
    {
        var client = new DataServiceClient();
        var gettingCities = await client.GetCitiesAsync();
        gvCities.DataSource = gettingCities;
        gvCities.DataBind();
    }));
}

The advantage of performing async operation this way over what we did earlier is that, it registers an asynchronous handler with the page and now the execution is not dependent on the synchronization context. Execution of the statements in the PageAsyncTask passed in is not dependent on page the page life cycle event. So, ASP.NET will not wait asynchronously after executing rest of the logic in the life cycle event. It would rather continue execution with the next life cycle event handlers. Statements in the registered task are performed whenever the dependent operation is finished.

Note that, we used the async keyword with a lambda expression. It is legal in .NET 4.5, because lambda is meant to create a single cast delegate with an anonymous method. To make the method executable asynchronously, we can use async and await with lambda.

 



ASP.NET Hosting - How To Make URL More User Friendly (URL Rewriting)

clock February 27, 2013 05:24 by author andy_yo

Sometimes we need a user friendly URL, but if we use query string variables, our site url will become messy.

Like we may be having

URL A: http://www.asphostportal.com/product.aspx?id=123456

and we want to have a URL for above as

URL B: http://www.asphostportal.com/product_123456.aspx

Obviously from above two the 2nd one is more user friendly and easily readable. Now the question is how to accomplish this task. The solution is very simple. In following I will be explaining the solution in detail

About ASPHostPortal.com

ASPHostPortal.com is Microsoft No #1 Recommended Windows and ASP.NET Spotlight Hosting Partner in United States. Microsoft presents this award to ASPHostPortal.com for ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2012, ASP.NET 4.5, ASP.NET MVC 4.0, Silverlight 5 and Visual Studio Lightswitch. Click here for more information

The Solution

Though there are more than one ways for this task but I will make use of the method "application_beginrequest" of "Global.asax". This method is called every time some request is made to the website.

Following will be the code to convert URL A into URL B in VB and C# programming language. You can modify this code as per your need.

VB

Public Sub application_beginrequest(ByVal sender As Object, ByVal e As EventArgs)
     Try
        Dim initialurl As String = Request.Url.ToString
        If initialurl.ToLower.IndexOf("product_") >= 0 Then
            Dim mTemp As String = initialurl.Substring(initialurl.LastIndexOf("_") + 1)
            Dim mProductID As Integer = mTemp.Substring(0, mTemp.LastIndexOf(".aspx"))
            Dim mOriginalURL As String = "product.aspx?id=" + mProductID
            Context.RewritePath(mOriginalURL)
        End If
        'Code to Log your Error
     Catch ex As Exception
     End Try
End Sub

CSharp (C#)

public void application_beginrequest(object sender, EventArgs e)
{
    try
    {
        string initialurl = Request.Url.ToString();
        if (initialurl.ToLower().IndexOf("product_") >= 0)
        {
            string mTemp = initialurl.Substring(initialurl.LastIndexOf("_") + 1);
            int mProductID = Convert.ToInt32(mTemp.Substring(0, mTemp.LastIndexOf(".aspx")));
            string mOriginalURL = "product.aspx?id=" + mProductID;
            Context.RewritePath(mOriginalURL);
        }
    }
    catch (Exception ex)
    {
        //Code to Log your Error
    }
}



ASP.NET Hosting - ASPHostPortal :: How to Modify Custom Error 404 in ASP.NET

clock February 14, 2013 11:43 by author andy_yo

One of the improvements We wanted to make after deploying our website were to add useful error pages. You know more than the YSOD (yellow screen of death). One of the major issues for me was creating a useful and informative 404 page. We wanted the page to tell the user why they got there, offer suggestions about what page they may be looking for, and allow them to search the site. So We did the development work committed the changes and had the CI machine push to the server (in the case app harbor).

1.       <system.web>

2.           <compilationdebug=truetargetFramework=4.0 />

3.           <customErrorsmode=OndefaultRedirect=~/error>

4.               <errorstatusCode=404redirect=~/404 />

5.           </customErrors>

6.       </system.web>

But We were still seeing the generic IIS sever errors! We did some searching on the internet and found a helpful property of the response object.

1.       Response.TrySkipIisCustomErrors = true;

 

About ASPHostPortal.com

ASPHostPortal.com is Microsoft No #1 Recommended Windows and ASP.NET Spotlight Hosting Partner in United States. Microsoft presents this award to ASPHostPortal.com for ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2012, ASP.NET 4.5, ASP.NET MVC 4.0, Silverlight 5 and Visual Studio Lightswitch. Click here for more information

By default IIS will see an response with an error status code (400-500) and will automatically change the content of the response to its default error page. What this property does it tell IIS not to do that if there is already content in the body of the page. Well that was easy, right? So We made the change and pushed it to the server, and navigated to a page that didn't exist. Well the server still returned me the default 404 page, but on a 500 it would give me my custom error page. So what gives? Well here is the deal, the way routing works is that if ASP.NET cannot find a file that matches the requested URL the request is given back to IIS to handle. So in the case of a 404 ASP.NET can't find the file so its given back to IIS who uses the default static file handler to serve the request. This would be slightly different if the route had matched but then say somewhere in our code we set the status to 404. In this case it would already be in the ASP.NET pipeline and ASP.NET would server the custom 404 page.

There are two ways to solve this problem. First is the easiest which is to open up IIS Manger and go to the “Error Pages” settings under IIS and change the 404 page there to use your custom 404 page.

custom_error.png

This is fine if you can remote into the server or you're not running in the cloud where multiple instances can be started. So how then do you make that work? Well lucky that We are using IIS8 hosting, starting with IIS7 and later these settings can be added to your web.config file under the System.WebServer node.

1.       <system.webServer>

2.           <httpErrorserrorMode=Custom >

3.               <removestatusCode=404subStatusCode=-1/>

4.               <errorstatusCode=404path=/404responseMode=ExecuteURL />

5.           </httpErrors>

6.       </system.webServer>

So lets dig into what some of this code means and tell you about the tricky parts that you need to know. Before you can add a custom page you need to remove the entry for the status code as there can only be one entry per status code. The tricky bit here is knowing to set SubStatusCode to –1. This basically means all the possible sub statuses. If you like you could remove only the specific one you needed and set only the specific one. Also if you are playing around with the config you might find that there is a defaultPath attribute on the httpErrors node. This defines a “default” error page should an entry not be found in the list. The problem is that by default this is “locked” and cannot be set in an applications web.config and instead needs to be set at the machine level.  Once you add these settings to your config you should be able to see your custom error page when you navigate to a page that does not exist.



ASP.NET Hosting - How to add favicon to your ASP.NET page

clock February 8, 2013 11:09 by author andy_yo

A favicon is the little image displayed at the left edge of the address bar in most browsers, or on the tab. Adding a Favicon is a nice way to enhance your website and give it a finished look.

About ASPHostPortal.com

ASPHostPortal.com is Microsoft No #1 Recommended Windows and ASP.NET Spotlight Hosting Partner in United States. Microsoft presents this award to ASPHostPortal.com for ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2012, ASP.NET 4.5, ASP.NET MVC 4.0, Silverlight 5 and Visual Studio Lightswitch. Click here for more information


Firefox displaying favicon:

Google Chrome displaying favicon:

 

Interner Explorer displaying favicon:

Adding a favicon to your website is very easy. First you need to create a 16 by 16 pixel icon and name it favicon.ico. No other file format or size will do. Save your favicon to the root directory of your ASP.Net web application:


In some older browsers (e.g., Internet Explorer 5), that's all it takes. For the latest versions of Firefox, Internet Explorer and Chrome, however, you need to add two links to your favicon to the head in your HTML. If you are using a master page, the same two links would go in the head on the master page.:

                <head runat="server">
                <
title>My Website</title>
                <
link runat="server" rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>
                <
link runat="server" rel="icon" href="favicon.ico" type="image/ico"/>
          </
head>


The reason you have two links to the same favicon is to make sure your favicon works with most browsers. Some, such as Firefox, will work with either link. Internet Explorer versions 6 and 7 are especially picky. If you use both links, however, one is bound to work in almost any browser.



ASP.NET 4.5 Hosting - Bundling/Minification and Embedded Resources in ASP.NET

clock February 6, 2013 10:41 by author andy_yo

If you want to share your application resources(like css, javascript, images, etc) between different projects then embedded resource is a great choice. Embedded resource is also good for component/control writers because it allows component/control writers to distribute all the application resources with just a single assembly. A lot of vendors are already using this approach. It will great for component/control writers if they can leverage the ASP.NET bundling and minification for improving the performance. So, in this article we will show you how to write a very basic component(helper) which will use the ASP.NET bundling and minification feature on embedded javascript/css files.

 

About ASPHostPortal.com

ASPHostPortal.com is Microsoft No #1 Recommended Windows and ASP.NET Spotlight Hosting Partner in United States. Microsoft presents this award to ASPHostPortal.com for ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2012, ASP.NET 4.5, ASP.NET MVC 4.0, Silverlight 5 and Visual Studio Lightswitch. Click here for more information

First of all create a new Class Library project and install the Microsoft.AspNet.Mvc, WebActivator and Microsoft ASP.NET Web Optimization Framework 1.1.0-alpha1(make sure to include the -Pre parameter in Package Manager Console) nuget packages. Next, add a reference of System.Web assembly. Then, create your control/component/helper. For demonstration purpose, We will use this sample helper:

01.          public static class HtmlHelpers
02.          {

03.                          public static MvcHtmlString NewTextBox(this HtmlHelper html, string name)

04.                          {
05.                                          var js = Scripts.Render("~/Test/Embedded/Js").ToString();
06.                                          var css = Scripts.Render("~/Test/Embedded/Css").ToString();

07.                                          var textbox = html.TextBox(name).ToString();

08.                                          return MvcHtmlString.Create(textbox + js + css);

09.                          }
10.          }

 

In this helper, I am just using a textbox with a style and script bundle. Style bundle include 2 css files and script bundle include 2 js files. So, just create 2 css files(NewTextBox1.css and NewTextBox2.css) and 2 javascript files(NewTextBox1.js and NewTextBox2.js) and then mark these files as embedded resource. Next, add a AppStart.cs file and add the following lines inside this file:

01.          [assembly: WebActivator.PostApplicationStartMethod(typeof(AppStart), "Start")]
02.          namespace Test

03.          {

04.                          public static class AppStart

05.                          {

06.                                          public static void Start()

07.                                          {

08.                                                          ConfigureRoutes();

09.                                                          ConfigureBundles();

10.                                          }

11.                                          private static void ConfigureBundles()

12.                                          {

13.                                                          BundleTable.VirtualPathProvider = new
                                                             EmbeddedVirtualPathProvider(HostingEnvironment.VirtualPathProvider);

14.                                                          BundleTable.Bundles.Add(new ScriptBundle("~/Test/Embedded/Js")

15.                                                          .Include("~/Test/Embedded/NewTextBox1.js")

16.                                                          .Include("~/Test/Embedded/NewTextBox2.js")

17.                                                          );

18.                                                          BundleTable.Bundles.Add(new StyleBundle("~/Test/Embedded/Css")

19.                                                          .Include("~/Test/Embedded/NewTextBox1.css")

20.                                                          .Include("~/Test/Embedded/NewTextBox2.css")

21.                                                          );

22.                                          }

23.                                          private static void ConfigureRoutes()

24.                                          {

25.                                                          RouteTable.Routes.Insert(0,

26.                                                          new Route("Test/Embedded/{file}.{extension}",

27.                                                          new RouteValueDictionary(new { }),

28.                                                          new RouteValueDictionary(new { extension = "css|js" }),

29.                                                          new EmbeddedResourceRouteHandler()

30.                                                          ));

31.                                          }

32.                          }

33.          }

 

The above class using WebActivator's PostApplicationStartMethod, which allows your assembly to run some code after the Application_Start method of global.asax. The Start method simply register a custom virtual path provider and two bundles which are used in our NewText helper class. But ASP.NET optimization framework will only emit a bundle url when debug="false" or when BundleTable.EnableOptimizations = true. Therefore, we also need to handle the normal javascript and css requests. For this case, the above method has also register a specific route for handling embedded resource requests using EmbeddedResourceRouteHandler route handler. Here is the definition of this handler:

01.          public class EmbeddedResourceRouteHandler : IRouteHandler
02.          {

03.                          IHttpHandler IRouteHandler.GetHttpHandler(RequestContext requestContext)

04.                          {

05.                                          return new EmbeddedResourceHttpHandler(requestContext.RouteData);

06.                          }

07.          }

08.          public class EmbeddedResourceHttpHandler : IHttpHandler

09.          {

10.                          private RouteData _routeData;

11.                          public EmbeddedResourceHttpHandler(RouteData routeData)

12.                          {

13.                                          _routeData = routeData;

14.                          }

15.                          public bool IsReusable

16.                          {

17.                                          get { return false; }

18.                          }

19.                          public void ProcessRequest(HttpContext context)

20.                          {

21.                                          var routeDataValues = _routeData.Values;

22.                                          var fileName = routeDataValues["file"].ToString();

23.                                          var fileExtension = routeDataValues["extension"].ToString();

24.                                          string nameSpace = typeof(EmbeddedResourceHttpHandler)

25.                                          .Assembly

26.                                          .GetName()

27.                                          .Name;// Mostly the default namespace and assembly name are same

28.                                          string manifestResourceName = string.Format("{0}.{1}.{2}", nameSpace, fileName, fileExtension);

29.                                          var stream =
                                             typeof(EmbeddedResourceHttpHandler).Assembly.GetManifestResourceStream(manifestResourceName);

30.                                          context.Response.Clear();

31.                                          context.Response.ContentType = "text/css";// default

32.                                          if (fileExtension == "js")

33.                                          context.Response.ContentType = "text/javascript";

34.                                          stream.CopyTo(context.Response.OutputStream);

35.                          }

36.          }

 

EmbeddedResourceRouteHandler returns EmbeddedResourceHttpHandler http handler which will be used to extract embedded resource file from the assembly and then write the file to the response body. Now, the only missing thing is EmbeddedVirtualPathProvider:

01.          public class EmbeddedVirtualPathProvider : VirtualPathProvider
02.          {

03.                          private VirtualPathProvider _previous;

04.                          public EmbeddedVirtualPathProvider(VirtualPathProvider previous)

05.                          {

06.                                          _previous = previous;

07.                          }

08.                          public override bool FileExists(string virtualPath)

09.                          {

10.                                          if (IsEmbeddedPath(virtualPath))

11.                                                          return true;

12.                                          else

13.                                                          return _previous.FileExists(virtualPath);

14.                          }

15.                          public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime
                             utcStart)

16.                          {

17.                                          if (IsEmbeddedPath(virtualPath))

18.                                          {

19.                                                          return null;

20.                                          }

21.                                          else

22.                                          {

23.                                                          return _previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);

24.                                          }

25.                          }

26.                          public override VirtualDirectory GetDirectory(string virtualDir)

27.                          {

28.                                          return _previous.GetDirectory(virtualDir);

29.                          }

30.                          public override bool DirectoryExists(string virtualDir)

31.                          {

32.                                          return _previous.DirectoryExists(virtualDir);

33.                          }

34.                          public override VirtualFile GetFile(string virtualPath)

35.                          {

36.                                          if (IsEmbeddedPath(virtualPath))

37.                                          {

38.                                                          string fileNameWithExtension = virtualPath.Substring(virtualPath.LastIndexOf("/") + 1);

39.                                                          string nameSpace = typeof(EmbeddedResourceHttpHandler)

40.                                                          .Assembly

41.                                                          .GetName()

42.                                                          .Name;// Mostly the default namespace and assembly name are same

43.                                                          string manifestResourceName = string.Format("{0}.{1}", nameSpace,
                                                             fileNameWithExtension);

44.                                                          var stream =
                                                             typeof(EmbeddedVirtualPathProvider).Assembly
                                                             .GetManifestResourceStream(manifestResourceName);

45.                                                          return new EmbeddedVirtualFile(virtualPath, stream);

46.                                          }

47.                                          else

48.                                                          return _previous.GetFile(virtualPath);

49.                          }

50.                          private bool IsEmbeddedPath(string path)

51.                          {

52.                                          return path.Contains("~/Test/Embedded");

53.                          }

54.          }

55.

56.          public class EmbeddedVirtualFile : VirtualFile

57.          {

58.                          private Stream _stream;

59.                          public EmbeddedVirtualFile(string virtualPath, Stream stream)

60.                          : base(virtualPath)

61.                          {

62.                                          _stream = stream;

63.                          }

64.                          public override Stream Open()

65.                          {

66.                                          return _stream;

67.                          }

68.          }

EmbeddedVirtualPathProvider class is self explanatory. It simply maps a bundling url(used above) and return the embedded request as stream. Note, this class will be invoked by ASP.NET optimization framework during bundling and minifying process. Now, just build your component/control/helper assembly. Then, create a sample ASP.NET(MVC) application and then use this component/control/helper in your page. For example, like,

[email protected]using Test.Helpers
[email protected]("New")

 



nopCommerce Hosting - nopCommerce benefits in E-Commerce Development

clock February 5, 2013 09:45 by author andy_yo

E-commerce is a very progressive sector today. Companies have come to understood that the internet is the next big thing for selling and are concentrating hugely on building their e-commerce brand. Several e-commerce softwares are already popular with users such as Joomla, Zencart and Magento but one e-commerce platform that is fast building its reputation is nopCommerce.

About ASPHostPortal.com

ASPHostPortal.com is Microsoft No #1 Recommended Windows and ASP.NET Spotlight Hosting Partner in United States. Microsoft presents this award to ASPHostPortal.com for ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2012, ASP.NET 4.5, ASP.NET MVC 4.0, Silverlight 5 and Visual Studio Lightswitch. Click here for more information

nopCommerce is ASP. NET 4.0 based open source e-commerce solution and it’s fast emerging as the preferred choice of e-commerce solution among web developers. This isn’t a mere coincidence and there are several reasons why nopCommerce is stealing a march over more established giants. In this reason, I will explain why nopCommerce is slowly emerging as the leader of the pack in the e-commerce sector.

The first reason why web developers have come to love nopCommerce is its ease of installation. Setup is extremely quick and the software is up and running within minutes. The software comes free and can be downloaded from the internet. It has mobile device support and one can add unlimited amount of products into the software. It supports both categories and manufacturers and any number of subcategories can be added.

A single product can be mapped onto multiple categories and this is great because often certain products tend to fall under 2 or more categories. It provides for anonymous checkout which is great for people who don’t wish to divulge their personal information and the checkout also comprises of just one single page which ensures that visitors aren’t heckled that in turn lead to greater conversions. It offers both multilingual and multicurrency support both of which are extremely cool add ons especially if you are looking to build a global e-commerce brand.

Real time currency exchange rates also ensure that consumers know exactly how much they are paying for in their own currency. The database can also be exported or import in an XML or Excel which at times can be really useful. PDF order receipts, live chat integration and W3C compliance, Open ID, Facebook and Twitter authentication are yet other powerful features that this e-commerce platform provides.

As far as navigation is concerned, it has a very good breadcrumb structure and you can also define your products better with the product tags and product attribute features. Your site is thus made SEO friendly making it more appealing to search engines to crawl and index. It also the product feed feature which automatically uploads new products into product search engines such as Google’s Froogle, Yahoo shopping, etc

The company has stayed true to its consumers and every new update is being made with even better customizations. nopCommerce is a platform here to stay and if you are keen in building your e-commerce website, then this a platform that you should definitely explore.

 



Windows ASP.NET Hosting - Boosting Web Application Performance with Bundling and Minification

clock February 1, 2013 10:34 by author andy_yo

The performance of your web application has a great impact on the end user experience. If your web application is slow, obviously users are going to be turned away from using it. There are many factors that contribute to the performance of a web site. A couple of important ones are - the number of requests sent from the browser to the server and the response size of each request. The newly added optimization features of ASP.NET 4.5 provide a neat way to bundle and minify JavaScript and CSS files thus taking care of the issues mentioned earlier. This article shows how these bundling and minification features of ASP.NET can be utilized in your web application.

About ASPHostPortal.com

ASPHostPortal.com is Microsoft No #1 Recommended Windows and ASP.NET Spotlight Hosting Partner in United States. Microsoft presents this award to ASPHostPortal.com for ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2012, ASP.NET 4.5, ASP.NET MVC 4.0, Silverlight 5 and Visual Studio Lightswitch. Click here for more information

What is Bundling and Minification

Consider an ASP.NET MVC web application that consists of a view named Index.aspx and makes use of the following JavaScript files:

  • jquery-1.6.2.js
  • jquery-ui-1.8.11.js
  • modernizr-2.0.6-development-only.js

By default these files are located in the Scripts folder and you refer them in your views as follows:

  1. <script src="../../Scripts/jquery-1.6.2.js"></script>
  2. <script src="../../Scripts/jquery-ui-1.8.11.js"></script>
  3. <script src="../../Scripts/modernizr-2.0.6-development-only.js"></script>

Now, when the view is loaded in the browser, the browser makes three independent requests to the respective files. If you observe the requests using Chrome Developer Tools you will see something like this:

Notice a couple of things:

  • The browser has sent three separate requests to respective .js files.
  • The total response size will be the sum of the file sizes of the individual files.

Also, notice the time taken to download these files.

The overall performance of the view can be improved if you bundle all three requests as a single request. This way instead of making three separate requests the browser will send a single request and still download the content of all three files. Performance will be further improved if you minimize the size of each file being downloaded by minification techniques such as removing white spaces and comments.

Default Bundling and Minification

Luckily, ASP.NET 4.5 provides inbuilt support for bundling and minification of files. The core functionality of bundling and minification is found in System.Web.Optimization namespace. If you create a new ASP.NET project you will find the following line of code in the Global.aspx file:

  1. protected void Application_Start()
  2. {
  3. ...
  4. BundleTable.Bundles.RegisterTemplateBundles();
  5. }

As you can see, the Application_Start event handler contains a call to the RegisterTemplateBundles() method that does the default bundling and minification for you. For default bundling and minification to work you need to specify the URLs of the JavaScript and CSS files a bit differently.

  1. <script src="../../Scripts/js"></script>

Notice the above <script> tag carefully. Instead of specifying a <script> tag per file there is only one <script> tag and the src attribute is of the form <script_folder_path>/js. This naming convention tells ASP.NET that all the *.js files from the Scripts folder are to be bundled together and minified. If you observe the request in Chrome Developer Tools you will find just a single entry for JavaScript files like this:

Notice the size of the downloaded content and compare it with the combined size of individual files.

For CSS files you would have used <css_folder_path>/css in the <link> tag.

  1. <link rel="stylesheet" type="text/css" href="../../Content/css" />

Though the syntax shown above works as expected, there is a small drawback. Let's say you refer JavaScript and CSS files in your views using the above syntax and your web application starts serving the requests. Sometime later you update some of these JavaScript and CSS files. Naturally, you expect the new script and styles to come into effect. However, the earlier files might have been cached by the browser or proxy server. Since the URL to the files is the same (src="../../Scripts/js" and href="../../Content/css") there is no way for the browser to detect whether the files have been changed or not. To rectify the problem it is advisable to use the following syntax :

  1. <script src="<%= BundleTable.Bundles.ResolveBundleUrl("~/Scripts/js") %>"></script>
  2. <script src="<%= BundleTable.Bundles.ResolveBundleUrl("~/Content/css") %>"></script>

The ResolveBundleUrl() method accepts the virtual path of the folder containing the script or CSS files. The ResolveBundleUrl() method not only generates a URL for src and href attributes but also appends a unique string token in the query string. This token is changed when a file changes, thus ensuring a unique URL for the changed files. The following figure shows how the string token is added in the query string:

Customizing Bundling and Minification

At times the default bundling mechanism may not meet your requirements. For example, you might have ten JavaScript files in a folder but depending on the usage pattern you may want to bundle them in two separate bundles of five files each rather than a single bundle. Also, you may want to bundle the files in a specific sequence based on their dependencies. Such a customization is possible through Bundle class. The following code added to the Application_Start event handler shows how the Bundle class can be used:

  1. protected void Application_Start()
  2. {
  3. ...
  4. var bundle = new Bundle("~/MyScripts");
  5. bundle.AddFile("~/Scripts/jquery-1.6.2.js");
  6. bundle.AddFile("~/Scripts/jquery-ui-1.8.11.js");
  7. bundle.AddFile("~/Scripts/modernizr-2.0.6-development-only.js");
  8. BundleTable.Bundles.Add(bundle);
  9. ...
  10. }

The above code creates a new bundle for virtual path ~/MyScripts. It then calls the AddFile() method to add specific script files. Finally, the newly created bundle is added to the Bundles collection. To refer the newly created bundle in views you will use the following syntax:

  1. <script
  2. src="<%= BundleTable.Bundles.ResolveBundleUrl("~/MyScripts")
  3. %>"></script>

In addition to creating a custom bundle as shown above you can also customize the overall bundling and minification process. To do so, you need to create a custom class that implements the IBundleTransform interface. You then need to implement the Process() method of the IBundleTransform interface and write a custom processing logic. The following code shows a simple implementation of the IBundleTransform interface that adds a copyright notice to the bundled content.

  1. public class MyBundleTransform:IBundleTransform
  2. {
  3. public void Process(BundleContext context, BundleResponse response)
  4. {
  5. StringBuilder sb = new StringBuilder();
  6. sb.AppendLine("// Copyright (C) 2012. All rights reserved.");
  7. sb.Append(response.Content);
  8. response.Content = sb.ToString();
  9. }
  10. }

The above code creates the  MyBundleTransform class, which implements the IBundleTransform interface. The Process() method receives two parameters viz. a BundleContext and BundleResponse. The Process() method then adds a copyright notice at the top of the bundled content and then reassigns the Content property. You can also get ahold of the individual files of a bundle using the response.Files property.

To use the MyBundleTransform class you modify the Application_Start event handler as follows:

  1. protected void Application_Start()
  2. {
  3. ...
  4. var bundle = new Bundle("~/MyScripts", new MyBundleTransform()); bundle.AddFile("~/Scripts/jquery-1.6.2.js");
  5. bundle.AddFile("~/Scripts/jquery-ui-1.8.11.js");
  6. bundle.AddFile("~/Scripts/modernizr-2.0.6-development-only.js");
  7. BundleTable.Bundles.Add(bundle);
  8. ...
  9. }

As you can see a new bundle has been created as before but this time an instance of the MyBundleTransform class is passed as the second parameter of the constructor. If you observe the resultant script in the Chrome Developer Tools, you will find the copyright line added at the top:

Summary

The newly added bundling and minification features of ASP.NET 4.5 make it easy to bundle your JavaScript and CSS files thus boosting the overall performance of your web applications. In order to avail the bundling and minification features you need to specify URLs to script and CSS files in a certain way. Following this naming convention automatically bundles all of the JavaScript and CSS files from a folder and serves them as a single request. You can also customize the bundling process using the Bundle class and IBundleTransform interface.



Cheap ASP.NET 4.5 Hosting

We’re a company that works differently to most. Value is what we output and help our customers achieve, not how much money we put in the bank. It’s not because we are altruistic. It’s based on an even simpler principle. "Do good things, and good things will come to you".

Success for us is something that is continually experienced, not something that is reached. For us it is all about the experience – more than the journey. Life is a continual experience. We see the Internet as being an incredible amplifier to the experience of life for all of us. It can help humanity come together to explode in knowledge exploration and discussion. It is continual enlightenment of new ideas, experiences, and passions


Author Link


 

Corporate Address (Location)

ASPHostPortal
170 W 56th Street, Suite 121
New York, NY 10019
United States

Tag cloud

Sign in