Bundling AngularJS HTML pages with ASP.NET

image_6Bundling and minification are two well-known techniques used to improve the load time of your website. These are especially important for sites that use extensively JavaScript to offer better user experience. There are plenty of tools to help you do bundling and minification of JavaScript and CSS files. If you are a .NET developer you are probably very used to live inside Visual Studio and expect it to offer you everything you might think of. In ASP.NET 4.5 you can use a bundling API to define how your files will be grouped and sent to the client. The following example demonstrates the usage of this API.

Then in your page (either an ASP.NET page or a MVC view) you would invoke the rendering of these bundles by calling Scripts.Render(“~/bundles/jquery”) and Styles.Render(“~/bundles/css”). Continue Reading…

Integrate external web services with slow responses

Sad userRecently I have been working on a project, which involved communication with a lot of external systems. These systems were mainly web services and databases. During the development phase of the project I only had access to a test version of these systems, i.e. dealing with test data. One of these external systems was of a greater interest as it was designed to serve the main data the customer dealed with. It was a traditional SOAP web service (build on ASP.NET). The service contains a bunch of methods to retrieve and update data. Everything seems pretty normal so far. However, a few of these methods that fetch data happened to work very slow. By working slow I mean, that the external server took its time to do the necessary computations. The calls to these methods were sometimes so slow that users had to see a waiting screen for 10 minutes. And this is pretty annoying. And the customer realized it. The first solution was, of course, to ask the customer if it was possible to optimize this service. Unfortunately, the service itself is developed and maintained by another company and it could take some time until a better version was released. We didn’t have this time, so we had to work ourselves on this issue. Continue Reading…

KnockoutJS Unobtrusive Validation with ASP.NET MVC

Knockout JSASP.NET MVC has a really nice capability of generating client-side validation code based on the data annotations on your model. It can either generate a JavaScript code with the rules or generate data attributes on the input elements (unobtrusive). By using the second approach jQuery can read these data attributes (by the unobtrusive validation plugin) and automatically wire the validation logic up on the client. All this works great if you have a completely standard application – a user enters some input data and sends it to the server. However, if your application also requires some client-side logic along, you may consider KnockoutJS. The basic functionality of KnockoutJS, however, is not enough when input from the user is required. Fortunately, there is a plugin, which adds validation capabilities to KnockoutJS. It provides a set of rules like min, max, equals, etc., which we can use out of the box. Now the issue is how to make the data attributes, generated by ASP.NET MVC, and KnockoutJS validation play together. As I couldn’t find anything out of the box, I decided to create my own plugin. Continue Reading…

ASP.NET localization with Umbraco

Umbraco is very neat and easy to integrate with ASP.NET, probably because it was built on it. One typical scenario is to create a multi-lingual web site with Umbraco, which automatically requires the use of localization. Umbraco supports localization very well and there are two main techniques, I can think of – using a dictionary or using a special page with custom fields. I personally prefer the latter, because one can use nicer names and descriptions for the custom fields, so it is easier and more intuitive to edit the texts.

Umbraco Localization Structure It is a good idea to create a separate branch in Umbraco for each language in your website. You should keep the language-independent settings in the root node and all language-dependent ones in the branches.

The next step is to use this localization in your custom controls in ASP.NET. Umbraco has a nice control, called item, which can display the contents of a property. By default, this control tried to look up the property in the current node, however you can also specify a node by its id. So, one solution is to use this control and specify the node ID of your Texts node every time (or create some wrapper over this control). However, I find this way very clumsy in spite of the nice properties the item control provides. Continue Reading…

Display Attribute Not Working With ASP.NET MVC 2 RTM

Recently I was asked about a strange problem concerning the Display attribute in ASP.NET MVC 2. The problem was the Display atribute had no effect in the view. It was really a strange one. I tried it myself and it was true.

Let’s have a look at that situation. Below is the model for a user.

Using Html.DisplayForModel() in our view results in the following web page:

As you can see the Display attribute has not been taken into consideration at all. The reason for this is ASP.NET MVC 2 RTN do not know about it. Display attribute is new for .NET 4 and ASP.NET MVC 2 RTM is compiled under .NET 3.5. This attribute is supported in Futures release of ASP.NET MVC and will be supported in any newer releases (like MVC 3).

OK, but I didn’t give you a working solution so far. Well, there is – DisplayName attribute. Note that it resides in System.ComponentModel not in System.ComponentModel.DataAnnotations. Our model now looks like this: