Many people combine all scripts (and style sheets) for the entire website into one bundle. The rationale behind this is the TCP connections limitation by browsers. Furthermore, once the bundle is downloaded, it gets cached by the browser and subsequent requests to the website are faster. This approach, however, works against the concept of modularity. Why changes in one file should invalidate the entire (potentially huge) bundle? Moreover, HTTP/2 removes the TCP connections limitation issue. Although, ASP.NET MVC provides a way to register resources separately, this build-in mechanism is not flexible enough. Continue Reading…
Category Archives: ASP.NET MVC
Model binding in ASP.NET MVC can be fun
Model binding is a technique that allows you to map data to your controller actions in ASP.NET MVC. In the old days one would manually call
1 |
Request["param"] |
to get the value of а parameter, but this could quickly become annoying as you get many parameters (f.x., via form POST). Model binding abstracts away this tedious activity and allows us to focus on designing our controllers. The process itself looks like this:
A nice bonus is that we can map our models from any kind of request, for example a form POST or AJAX. Continue Reading…
Double confirm identity with ASP.NET Identity
ASP.NET Identity brings the authentication and authorization to a new level. Based on the OWIN middleware, one can plug & play different authentication and authorization providers, f.x. OAuth, OpenID, simple forms authentication. The beauty of the new identity model is that it provides a unified interface to work with. One can write her own log-in providers that still rely on the same unified interface.
Besides the typical scenarios where a user logs in with her username and password, or Twitter/Facebook/Google, or maybe a two factor log-in with SMS/e-mail confirmation, there is another interesting one. In some cases you would want to enable users to log in with simple forms authentication to access a basic area of functionality, f.x. just reading data. On the contrary you would want to protect other areas of the web site where the user can edit sensitive information, f.x. his password. In this case you may want to require that the user logs in again – either with the same provider or with another one.
To solve this issue I decided to write a simple extension for ASP.NET Identity, which I called Double confirm identity. It provides a set of utility methods you can use to achieve the described scenario.
KnockoutJS Unobtrusive Validation with ASP.NET MVC
ASP.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…
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.
1 2 3 4 5 6 7 8 9 |
public class User { [ScaffoldColumn(false)] public int Id { get; set; } [Required] [Display(Name = "User name")] public string Name { get; set; } } |
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:
1 2 3 4 5 6 7 |
public class User { ... [Required] [DisplayName("User name")] public string Name { get; set; } } |