LATEST POSTS
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 ReadingCreating 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 ReadingReturning 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 ReadingImplementing 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 ReadingCreating 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 ReadingDependency Injection in WCF using Castle Windsor
Dependency Injection (DI) is a design pattern in Object Oriented Programming where the dependencies of a class are injected at runtime. These dependencies are mostly injected through a constructor (Constructor DI) or a property (Property DI). One of the main advantages of DI is it simplifies unit testing by allowing to inject mock dependencies in the place of real classes. In some cases we should have a default constructor in classes like in ASP.NET MVC controllers, WCF services etc. In WCF the service classes should have a default constructor else the framework will throw a runtime exception. To solve this problem developer ends up writing two constructors one is default and the other one is parameterized. The default constructor is used by the framework and the parameterized one is used by developers at the time of unit testing.
Although having two constructors suffice the job but we are violating the principles of dependency injection by instantiating the concrete classes in the default constructor. WCF is a highly extensible framework and fortunately it provides extension points even to customize the service instantiation. It looks like some of the DI containers simplify our job of customizing service instantiation by providing their own custom service host factories.
In this article we are going to see about the WCF extension point IInstanceProvider that allows us to customize the service instantiation and how the DI container Castle Windsor simplifies the task of creating services without default constructor through its new integration facilities.
Continue ReadingUsing NAnt on creating Windows Services
Software development frequently involves doing a set of repeated actions in day-to-day life. If you take a web developer some of the activities he does frequently are building the web application and the referenced projects, running unit tests, generating documents, packaging the source files, copying the package to FTP server etc. Typically in big projects these activities occur numerous times. Many times developers end up doing these activities manually due to mostly lack of knowledge on automation and build tools. Build tools plays a crucial role in Continuous Integration where every time a developer checks-in his changes, a set of actions will take place automatically through build scripts to make sure the latest code won't create any build issues.
I recently started exploring NAnt when I was developing a couple of windows services for a client who asked me to write build scripts for those services. I was not much aware of automation and NAnt before so I thought these scripts are just going to help at the time of deployment to build the services without Visual Studio and to install them easily without much trouble. Soon I started to realise how much these scripts helps to save time and effort at the development phase. In this article I planned to explain how to take the advantages of build scripts while developing windows services.
Continue ReadingFirst look at ASP.NET MVC
The ASP.NET MVC is a web application framework developed by Microsoft that implements the model-view-controller (MVC) pattern. It was first released in April 2009. ASP.NET MVC framework is a lightweight, highly testable presentation framework that is integrated with existing ASP.NET features. Based on ASP.NET, it allows software developers to build a Web application as a composition of three roles: Model, View and Controller.
The ASP.NET MVC Framework couples the models, views, and controllers using interface-based contracts, thereby allowing each component to be easily tested independently. In this presentation we are going to see about the important advantages and features of ASP.NET MVC.
View PresentationHow to know whether an email is delivered and read
Sending email is very important in many web applications and most web technologies provides built-in classes for that. Microsoft.NET comes with a built-in class named SmtpClient just for that. Although sending email is very easy, because of the nature of communication the difficult part is to know whether the email is successfully delivered and read by the recipient. There are different approaches available to know the delivery status of the email but nothing solves the problem 100%.
In this article I’m going to discuss about the two important ways that are frequently used to know the delivery status of email, one is using read-receipts and the other one is appending an image-tag with the email body.
Continue ReadingUnit Testing using NUnit and Rhino Mocks
The smallest piece of a software that can be testable is called as a Unit. In Object Oriented Programming languages like C# each class is called as a Unit. Testing each such unit independent of others is called as Unit-Testing. Unit Testing is very important in Test Driven Development (TDD) where the development starts with writing test cases before code. Lot of testing frameworks are available for doing better unit testing in different languages. For .NET there are frameworks like NUnit, TestDriven.NET, xUnit.net and more.
Usually a class has dependencies with other classes. Unit Testing is all about testing the functionalities of the class in test and not its dependencies. In order of using the real instances of the dependencies we can use dummy or mock objects. NUnit itself comes with mocking support but there are plenty of other frameworks like Rhino Mocks, NMock, TypeMock.NET etc. that does the job better than NUnit. Some of them are open-sources and some are paid. I usually go with Rhino Mocks, it’s an open source and easy to use.
In this article we are going to see about how to do better unit testing in .NET using NUnit and Rhino Mocks.
Continue Reading