POSTS ARCHIVED ON "MARCH 2012"

How to change the input parameters using action filters

Filters are behaviors that can be added to different stages in the ASP.NET MVC request processing pipeline. There are four different types of filters: Authorization, Action, Result and Exception. The Action filters are the ones that are called before and after an action is executed.

One of the interesting thing with the action filters is, we can even change the parameters before passed to an action and that’s what we are going to see in this article.

Continue Reading

Creating custom route constraints in ASP.NET MVC

In ASP.NET MVC the Routing Module is the URL routing engine that directs the incoming requests to controller actions. There are cases where we need to impose constraints over the incoming requests that are mapped routes. These constraints are used to avoid from directing invalid requests to the controller actions.

In this article we are going to see how to create a simple custom route constraint and supply it to a mapped route.

Continue Reading

Returning data and view from a single controller action

In ASP.NET MVC the controller is the component that receives requests from the client, performs some actions and returns results back to the client. The controller typically returns results as data or views. The ASP.NET MVC 4 beta comes with a new type of controller called ApiController that helps to create RESTful applications in an easy and efficient way. So having two types of controllers now experts advise to use the default controller for returning views and the ApiController for returning data. Since the default controller itself capable of delivering both data as well as views I don’t see many benefits by introducing the new ApiController. It would be great if the default controller has been extended to add the RESTful features into the MVC framework.

In most of the MVC applications we have controllers that return both data and views. The problem is that from a single controller action we can’t return both data as well as views. Many times we end up writing two controller actions one to return view and the other to return data in either JSON or XML format (for AJAX calls). Having two controller actions makes more chances of code duplication. It would be really nice if we can return both data as well as view from a single action itself. Doing this stuff is quite difficult in ApiController because it doesn’t have capabilities to return views. So we left with the option of using the default controller to achieve that.

Continue Reading

Implementing an XSLT View Engine for ASP.NET MVC

ASP.NET MVC 3 is a highly extensible framework that allows us to replace most of the built-in parts in the processing pipeline with custom implementations. As default it comes with two standard view engines: Razor and the Web-Forms view engines. The interesting thing is the framework provides extensions that allow us to register even our own custom view engines.

In some web applications the business model is available as XML and developers prefer to use XSLTs for rendering data as HTML. In those cases instead of using the built-in view engines it would be a nice choice to use a XSLT engine that renders the data as HTML. There are couples of XSLT view engines already available: one at MVCContrib and the other at NuGet. In my current project I need a mini XSLT view engine so instead of using the existing ones I implemented one myself.

Continue Reading

Creating a REST service using ASP.NET Web API

A service that is created based upon the architecture of REST is called as REST service. Although REST looks more inclined to web and HTTP its principles can be applied to other distributed communication systems also. One of the real implementation of REST architecture is the World Wide Web (WWW). REST based services are easy to create and can be consumed from a wide variety of devices.

There are many APIs available in different languages to create and consume REST services. The ASP.NET MVC beta 4 comes with a new API called ASP.NET Web API to create and consume REST services. While creating REST services it is important to follow the rules and standards of the protocol (HTTP). Without knowing the principles of REST it is easy to create a service that looks RESTful but they are ultimately an RPC style service or a SOAP-REST hybrid service. In this article we are going to see how to create a simple REST service using the ASP.NET Web API.

Continue Reading