Statistics
Visits: 105080
Page Visits: 139134
Active visitors (monthly): 5364
Feed Count: 45
Most visited page: Adding value as a technical analyst
Category : ASP.NET MVC

Anonymous profile initialization in Session start

My blog application uses an unusual way to measure anonymous visitors coming to the blog. First, I have turned anonymous identification ON in web.config. In anonymous profiles, I track the IP address of visitors in Session start (global.asax). And in a statistics component, I count the number of active profiles. Active profiles are visitors who are active in the last month.

After MVC3 was deployed, this feature stopped working. For some reasons, anonymous profiles were not initialized in Session start event handler in global.asax. So, the profile - HttpContext.Current.Profile was returning null.

After doing some research, I realized that the MVC controller was having the right profile object. Controllers have a property called Profile which exposes the anonymous profile object and this was working fine. As a workaround, I have used the profile object in the initialize method of the HomeController to initialize the anonymous profile. So, the active visitor count is working fine again.

I suspect not initializing the profile object (HttpContext.Current.Profile) in session start event is an unintended change from Microsoft.

Permalink | No Comments | Leave your comment
 

Forms Authentication in MVC

Authentication in MVC is controlled by the [Authorize] attribute. If this attribute is put in front of a controller action, the unauthenticated user will be redirected to the login form. Usually, the URL will be something like http://App/Login?ReturnUrl=/App/Page. After authentication, the user will be redirected back to the /App/Page Url.

If you are writing custom FormsAuthentication, this does not work. Usually, the user will be redirected to the default redirect page as specified in the web.config. Here is a forum post describing this situation: http://stackoverflow.com/questions/4246606/formsauthentication-getredirecturl-always-returns-the-default

As described in the post, a hidden form element can do the trick. But, it does not work, when the user retries a login (after wrong password). To make it work, ensure that ReturnUrl is present in the ViewData and passed along multiple invalid attempts:


<% if (ViewData["ReturnUrl"] != null)
 { %>
       <%= Html.Hidden("ReturnUrl",  ViewData["ReturnUrl"].ToString() )%>
 <% } %>

Permalink | No Comments | Leave your comment
 

Notes on Asp.Net MVC

Here are some notes about Asp.Net MVC:

Asp.Net MVC is modelled around Model2 (Struts-like) framework based on the original MVC. The controller processes the HTTP requests, finds a View and passes the ViewModel to the View. The View updates itself with the ViewModel.

In Webforms, the Page class is the HTTP Handler. MVC requests are handled by MVC Handler. MVC Handler examines the URL to identify the right controller factory and the right controller. The controller has an execute method that does most of the processing. Internally the controller uses ActionInvoker to identify the action and invoke the action. Action selectors are attributes that can help identify the right action to select for a request. Action filters operate before and after the method call and does things like authorization, exception handling, etc. The result of the action method call is wrapped into an ActionResult object. The ActionResult has an ExecuteResult method that provides the final response to the browser. ActionResult can be ViewResult, JSONResult, FileResult, EmptyResult etc. It is upto the View engine to process the ActionResult to produce meaningful HTML / Javascript to the browser.

All aspects of MVC is customizable - ControllerFactory, Controller, ActionInvoker, ActionFilter, ActionSelector, ViewEngine, ViewResult, ModelBinder. 

The default View engine in MVC is the WebformsViewEngine. This means that master pages, content pages, user controls, themes, will work well in MVC. Because MVC does not have a page postback lifecycle, events on controls will not be triggered. Server controls are generally not recommended in MVC. Server controls generate ViewState which is of no use in the MVC world. MVC uses Helper classes to generate HTML. Helpers exist for both HTML, AJAX. However, I would recommend using JQuery extensively in MVC apps instead of the helper classes.
 

Till Microsoft supports MVC with a MVCViewEngine with a set of server controls suited for MVC, the adoption of MVC will be slow. SparkViewEngine has a set of HTML tags that can fill this void, for the time being. Maybe MVC 3 will have more UI features like Intellisense for controllers, actions etc. R# (Resharp) is a third-party plug-in that provides similar features.

Update: Razor in MVC 3 seems to be what we need in a ViewEngine. Will be checking, if I can migrate this blog to use MVC with Razor.
 

Permalink | No Comments | Leave your comment
 

Lambda Expressions

A little known gem in C# is the lambda expression. Lambda expression is used to define function delegates. A function delegate is defined as:

public delegate TResult Func<T, TResult>(T arg);

In the above code, Func is a delegate which has a templated argument T and returns TResult.

Lambda expressions are used to define function delegates. For eg, the following code defines a Func delegate as lambda expression. Here, n >0 is a function that returns true if the integer n is positive.

Func<int, bool> Positive = n => n > 0;

Lambda expressions are used a lot in ASP.NET MVC.

Permalink | No Comments | Leave your comment
 

ASP.Net MVC - A first look

I am starting to go over the ASP.Net MVC Tutorials. If you do not know anything about ASP.Net MVC applications, here is what happens when you use a ASP.Net MVC application:

When you type in a URL like http://localhost/MvcApp/Home, a HomeController is created. The HomeController class will have various "Action methods". One of the methods is Index(). This method will create a Model object -- with data usually from database. Then the method will create a View object passing the Model object to the view. The user interface for the View is HTML markup that can be found in /Views/Home/Index.aspx. The HTML markup is similar to the ASP styled code with <%= %> tags.The View knows how to fill itself with the Model data.

With ASP.Net MVC applications, there is no Webserver controls like TextBox, DropDownList. There is no GridView type controls. There is no validation controls. Controls from AJAX Control Toolkit cannot be used. All that you have is ViewHelper classes like Html! ASP.Net MVC is a new direction for ASP.Net developers, and we should wait for VS 2010 and support for MVC especially in the HTML markup front.

Permalink | 1 Comment | Leave your comment
 
Commented by Vijay at 15-Apr-2010 01:48 PM

There is lot of good work to be done in MVC before it becomes really popular