Category: ASP.Net MVC


Create and Run a Docker Container for ASP.Net MVC4 Application using Azure Windows Server 2016 VM

In this tutorial, we are going to see how to create a Docker Image for an ASP.Net MVC4 application. We will containerize the created image, run it locally and test it. For hosting an ASP.Net MVC4 application we need IIS, so we need a Windows Container (IIS doesn’t run on Linux container). To work with Windows Containers, we need either Windows 10 (Anniversary update) or Windows Server 2016. We are going to use Windows Server 2016 Datacenter VM running in Azure as our development environment.


Convert an ASP.Net MVC Webpage HTML to PDF

Converting a webpage or plain HTML to PDF helps in sharing the visualized data through different channels like email attachments etc., In this tutorial we are going to see how to convert an ASP.Net MVC Webpage or plain HTML to PDF document. We will use SelectPDF Community edition to convert an ASP.Net MVC View to PDF. There are many other options to convert HTML to PDF, but SelectPDF is easy to integrate with MVC application and its API can handle HTTP Cookies, HTTP Headers etc with good performance.


Display session expire popup in ASP.Net MVC

In this tutorial, we are going to implement session expiry popup in an ASP.Net MVC application. It is most common requirement to alert user by showing a warning popup ahead of time before the actual session expires. We use Timeout-dialog.js JQuery plugin by Rodrigo Neri (We specifically take Michael Khalili’s timeout-dialog’s fork) to show the session expiry popup. Popup will be having an option to stay signed-in otherwise user will be logged out on Session expiration.


Print a Webpage with CSS in ASP.Net MVC

In this tutorial we are going to see how to print a webpage which is developed in ASP.Net MVC. The usual problems which we face in printing a webpage is with the rendering of CSS on print version. Also different browsers behave differently while printing pages with background styles and images. We are going to use traditional JavaScript’s window.print() to trigger print functionality.


JumpStart # 42 – Download file in ASP.Net MVC

In this jumpstart, we are going to see how to download a file from server to client browser in ASP.Net MVC using a RouteHandler . A RouteHandler will be created to handle all the requests made to a particular URL with specific route data (part of URL itself) to identify the file requested and will return a particular HttpHandler for the request. HttpHandler is going to find the file on the server, set it into the Response of HttpContext and finally send the response back to the client (typically a browser, but it can be anything).


JumpStart # 36– Access IIS Express Localhost URL outside development machine on any other Remote device within same Network

In this short Jumpstart, I am going to show on how to access IIS Express (Visual Studio Development Server) Localhost URL outside the development machine on any other device which is connected to the same network as of Development machine. This would help us in saving lot of time in testing the application on different devices with different form factors. We often use emulators to test the behavior of our application, though this approach would give prospective results, one cannot trust emulators with 100% confidence (take an example of File upload). If there was a way to directly access the application (without hosting it, directly from development server) on the remote devise of interest, we can test and be confident about application usage.


Simple C# Application to Remove/Clean Unused/Orphaned Resource file entries

In most of the present day code development, Application Localization is being achieved using RESX files. As time progress, we might not be able to keep track of which key-value pairs of a RESX file are being consumed by the application and which are orphaned. At present, it is very difficult to remove the orphan entries through automation and most of us stick to manual clean up. There are some tools which are capable of cleaning RESX files, but they come with price tag. In this quick tutorial, I am going to show how to clean a resource file through regular C# code.


JumpStart # 33– Ignore a property of Model from EditorFor Scaffolding in ASP.Net MVC

In this JumpStart, I am going to show on how to prevent automatic scaffolding of a Model Property while using @Html.EditorFor extension in ASP.Net MVC.

Many times we might create a Model (what I was referring here is a typical ViewModel) which will have properties for internal usage of Controller/Business layer. That means they are not intended to display on UI. But when we use @Html.EditorFor, even those properties will be scaffold automatically and displayed on UI.


Export ASP.Net MVC View to PDF in 3 Quick steps

I often come across questions related to generating PDF file from HTML or converting ASP.Net MVC page to PDF at runtime. These days I do see the frequency of these queries increased and decided to write a small solution to this problem. Most of the solutions around this topic involves lot of code using components like ITextSharp. Even if we invest such an effort and solve the problem, it will still be (sometimes) a non-generic solution and will land up refactoring the code for achieving reusability. In this tutorial, I am going to share a quick solution that is flexible (at least in my perspective) and mostly reusable.


JumpStart # 32– Prevent ASP.Net Page to load in Cross-domain IFrame

In this short jumpstart, I am going to show how to prevent loading of an ASP.Net MVC page in a cross domain iframe using traditional HttpModules. We use HTTP header “X-Frame-Options” to prevent the loading of page in iframe, but instead of injecting this header on every code file, we use a HttpModule to configure this header as a site wide option. Here we use X-Frame-Options to “Same Origin” so that iframe requests from same domain will be served and rest all ignored.


Session Management in Azure WebRoles using Azure Cache Service

Traditional ASP.Net developers can’t imagine their life without the usage of Session Object (either – InProc or State Server or SQL Server or Custom modes). Devs use this powerful API not only to persist sensitive information on server for some time intervals, but also to transfer data objects across postbacks between different pages.

Problems with session management arises when we go on to the Webfarm (or typically multiple instances of web server), especially where a typical load balancer comes in between the client and server farm. When a ClientA makes a first request, which is being routed to ServerA, then ServerA creates a session object. But what if simultaneous requests from ClientA goes to ServerB? As Session object is in ServerA, there will be Null Object reference exception getting thrown.


Diagnose your ASP.Net MVC application using Glimpse

Glimpse is an open source free diagnostics platform through we can understand our web applications performance and diagnose key areas of our code. Glimpse gives a intuitive UI through which we can examine server execution times, server configuration, in/out data etc., which will help us to improve performance of our application. More over getting started with glimpse is very easy, one can get a Glimpse report fairly in 3 quick steps.


JumpStart # 22– Image preview for input file type using HTML5 File API

In this short tutorial we are going to see how to preview an image when selected for upload using input file type. We use HTML5 File API and its FileReader to read the file and display it in the image tag. FileReader’s readAsDataURL will start reading the contents of the specified Blob, which may be a File. When the read operation is finished, onloadend callback will be called and the raw binary data of the file contains in the result attribute.


JumpStart # 19– Setting up Dependency Injection for ASP.Net Web API

We use Unity IoC container to achieve dependency injection for Web API. Create a ASP.Net MVC4 Project and its associated Class Library as shown in this tutorial –

Instead of installing Unity.Mvc3 Nuget Package, install Unity.WebAPI Nuget Package.


Dependency Injection for Multiple Concrete Implementations of an Interface

Dependency Injection and Inversion of Control (IoC) are one of the key players in todays web architectures. DI improves code reusability and guarantees loosely coupled systems. Having a DI onboard will help in both code maintenance perspective and application testing.

Following tutorial narrates on how use Unity Container (typically an IoC) with ASP.Net MVC4 web project. At a glance we will see how to setup Unity container along with its implementation on multiple concrete implementations of an interface.


ASP.Net MVC authentication against Windows Azure Active Directory (WAAD) with Access Control Services (ACS)

In this tutorial we are going to see how we can authenticate users who want to use our services with Windows Azure Active Directory (WAAD) through Access Control Services (ACS). This tutorial is going to narrate a step by step process, I personally suggest to implement all steps in order wise.

Windows Azure Active Directory (Windows Azure AD) is a modern, REST-based service that provides identity management and access control capabilities for your cloud applications. In simple words it is a service that is made available through Windows Azure so that you can manage your organization’s cloud directory where Microsoft is going to take care of AD’s scalability.


Lazy loading of Images in ASP.Net MVC with RouteHandler and JQuery

In this short tutorial, I am going to narrate on how you can lazy load images in a ASP.Net MVC 4 page. In most of todays social media sites, we often come across lots and lots of images which kill most of the bandwidth unnecessarily. So keeping to optimize the rendering of images only ON DEMAND would be a good solution in both bandwidth optimization and performance perspectives.

In the code below, I am going to demonstrate on how to create a specific Route Handler in ASP.Net MVC 4, which returns an instance of HttpHandler. HttpHandler is going to serve image to the client browser, but to make client browser not to understand actual image URL, we have used Route Handler. Later we use JQuery LazyLoad plugin to get images only on demand i.e., only on focus. We have wide range of effects and different configuration on LazyLoad plugin, but I am settling for simple fade effect for making this tutorial simple and elegant.