Showing posts with label ASP.NET MVC. Show all posts
Showing posts with label ASP.NET MVC. Show all posts

Tuesday, July 1, 2014

What's new in ASP.NET MVC 5?

ASP.NET MVC 5 is a framework for building scalable, standards-based web applications using well-established design patterns and the power of ASP.NET and the .NET Framework. Below is the list of features which are bundled in ASP.NET MVC 5.

1) One ASP.NET

MVC Project templates are integrated very efficiently with one ASP.NET experience.  Microsoft has finally brought all the ASP.NET related project templates together, and the result is One ASP.NET.  

The figure depicted below shows, how the web templates in Visual Studio 2013 are now pointing to a single web application.  You can also combine multiple projects like Web API, MVC etc. in case you’re designing a complex system.

2) ASP.NET Identity

MVC project templates have been updated to use ASP.NET identity for authentication and identity management.  You can easily configure the type of authentication at the initial stage of the project and MVC would configure all the required settings accordingly.
As shown in the screenshot, 3 choices are provided:

No authentication: If user does not want to implement any authentication or implement a custom authentication

Individual User accounts: Allows you to have full control of user accounts in your application.  Users can create their account or sign in through social media sites like Facebook, Google etc.

Windows Authentication: If user wants to implement windows authentication mode in their web application

3) Bootstrap

MVC project templates have been updated to use bootstrap CSS framework to provide a slick and responsive UI for your application which can be easily customized.  Bootstrap makes front-end web development faster and easier. It's made for folks of all the skill levels, devices of all shapes, and projects of all sizes.

MVC 5 now provides a neat responsive UI when creating a new web application as depicted in the screenshot below.  The layout on the right is for mobile devices.

4) Authentication filters

Prior to ASP.NET MVC 5, there were 4 types of filters:

  1. Authorization filters
  2. Action filters
  3. Result filters
  4. Exception filters
Previously [Authorization] attribute was used to enforce role based security.

MCV 5 introduces new kind of features called authentication filters, which run prior to authorization filters.  The main advantage of using authentication filter is, it runs prior to authorization filters.  It allows you to specify authentication logic at action, controller or global level.

The main purpose of adding authentication filters is to separate authentication from authorization (first authenticate, then authorize)
  • Authentication verifies who you are
  • Authorization verifies what are you authorized to do
Here is a short overview of how to implement your custom action filter:

5) Filter overrides

Filters can now be overridden for a given action or controller by applying override filter attribute.   Override filters specify a set of filter types that should not run for a given scope (action or controller).  This allows you to add global filters, but then exclude some from specific actions or controllers.

There are different types of override attributes in ASP.NET MVC 5, which allows you to clear filters by their type:

  1. [OverrideActionFilters]
  2. [OverrideAuthentication]
  3. [OverrideAuthorization]
  4. [OverrideExceptionFilters]
  5. [OverrideResultFilters]

The example given below, applies “OverrideAuthorization” filter to Index action, which would exclude authorization filter and would override Authorization filter applied at the controller level.



6) Attribute Routing

Routing is the term used by MVC to match a URI to an action. MVC 5 now supports a new type of routing called attribute routing.  As the name suggests, attribute routing uses attributes to define routes.  It gives you more control over the URIs in your web application.

In the previous versions of MVC, rules were set in RouteConfig.cs file like shown below:

The example given above, can be simply defined in MVC 5 like this:

Attribute routing can be enabled by using a single line of code in RegisterRoutes:


Monday, January 13, 2014

RequireJS and ASP.NET MVC

RequireJS is a javascript file and module loader.  It is used to load your javascript frameworks/plugins dynamically when needed and can optimize the time taken by your website to load your pages.

I'll demo a simple ASP.NET MVC 3.0 Application with RequireJS

Step 1: Download latest version of RequireJS from

Step 2: Create an ASP.NET MVC project and include the require.js file in your _Layout.cshtml page or Site.Master if you're using ASP.NET MVC 2.0.

<script src="@Url.Content("~/Scripts/require.js")" data-main="/scripts/main.js" type="text/javascript"></script>

data-main  attribute refers to main.js file which would act as a single point of interaction.  You can load all your javascript files dynamically through this js file.  This attribute would load the main.js file asynchronously after require.js file is loaded.

Step 3: Include jquery file in your scripts folder

Step 4: Add a div with the id="container" in a view

Step 5: Below is the code for Main.js file

require(['Scripts/jquery'], function () {
    $().ready(function () {
        $('#container').html("Hey! Require.js rocks!!");

The code above loads jquery file dynamically and initializes the document.ready event.  The code would simply insert a static html inside "container" div.

Note that "Scripts/jquery"  would be your path of jquery.js file excluding the ".js" extension.

For example, if you are refering to a file jquery-1.10.1.js located in scripts folder, you have to use require(['Scripts/jquery-1.10.1'],....)

Thursday, December 5, 2013

Get Compile-Time View Errors in ASP.NET MVC

ASP.NET MVC developers generally face an issue with compiling their views.  The errors are not reported when compiling views and they eventually turn out to be "Yellow Screen Of Death" issues from client.  Below are the steps to enable compilation errors in views:

  1. Right-click on your project file in Visual Studio’s Solution Explorer and choose Unload Project.
  2. Right-click the project file again and choose Edit YourProject.csproj.
  3. Look for a node called <MvcBuildViews> in the first <PropertyGroup> node (the one without any other attributes). If it’s not there, add it.
  4. Add or update the value inside the <MvcBuildViews> node to true. When you’re done it should look like this: 

  5. Save the project file changes.
  6. Right-click the project file in Solution Explorer one last time and select Reload Project.

Wednesday, November 27, 2013

Handling Dynamic Subdomains in ASP.NET MVC

I was doing some research work on implementing dynamic subdomains in an ASP.NET MVC, and I found an interesting article which explains the routing functionality and a trick to test it on local environment:

Thursday, November 14, 2013

ASP.NET Force HTTPS for all the pages of website

Most of the secured sites have a scenario of permanently redirecting to HTTPS protocol, even if the user tries to access it through HTTP.  

You can easily achieve this by adding a small piece of code in your Application_BeginRequest event in Global.asax file.

  protected void Application_BeginRequest(Object sender, EventArgs e)
            if (ConfigurationManager.AppSettings["HTTPS"] == "1" && HttpContext.Current.Request.IsSecureConnection.Equals(false) && HttpContext.Current.Request.IsLocal.Equals(false))
                Response.Redirect("https://" + Request.ServerVariables["HTTP_HOST"] + HttpContext.Current.Request.RawUrl);

The Above code would exclude HTTPS redirection for local environment i.e. localhost too.