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 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