Please enable Javascript for better experience...
BundleConfig, FilterConfig and RouteConfig in MVC
By Rahul Kumar Jha | Jan 15, 2015 | In Tutorials | Update: Mar 2, 2015 | Total Views [ 37705 ]
Taged In
(11 Like)
Rate

In this part of tutorial you will see how to add BundleConfig, FilterConfig and RouteConfig files in MVC and how they are useful in application.

Introduction

MVC architecture is totally different from other web applications like Asp.Net and this difference can be noticed while running application. MVC application generally don't run with file extensions. Till now we added controllers and views in our MVC application but if you try to run it, can face a big error. You need to define routing pattern so that request can be processed properly. Let's add these files one by one.

Step by Step

Follow below steps to add BundleConfig, FilterConfig and RouteConfig files in your application.

RouteConfig.cs

To add RouteConfig file follow below steps:

Step 1: Right click App_Start folder and click Add > Class

New popup box opens. Name your class as RouteConfig.cs.

Step 2: Replace class with below code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace DotNetConceptCreateMyFirstMVCProject
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

BundleConfig.cs

To add BundleConfig file follow below steps:

Step 3: Same as step 1.

Step 4: Replace class with below code.

using System.Web;
using System.Web.Optimization;

namespace DotNetConceptCreateMyFirstMVCProject
{
    public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.validate*"));

            bundles.Add(new StyleBundle("~/Content/css").Include(
                      "~/Content/bootstrap.css",
                      "~/Content/site.css"));

        }
    }
}

FilterConfig.cs

To add FilterConfig file follow below steps:

Step 5: Same as step 1.

Step 6: Replace class with below code.

using System.Web;
using System.Web.Mvc;

namespace DotNetConceptCreateMyFirstMVCProject
{
    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }
    }
}

Merely adding these file and code doest not solve the problem. You need to register these into Global.asax file. In next chapter you will learn registering these files to global.asax file.

<< Adding Controller and Views to MVC
Register Bundle, Route and Filters in Global.asax file in MVC >>

Share this

About the Author

Rahul Kumar Jha
Rahul Kumar Jha
Founder, Developer dotnet-concept.com

Public profile: user/profile/99900001


Has working experience in different phases of Software Development Life Cycle (SDLC) in CMS, Gaming, Health Care and Financial Services domain using Agile pattern. Working experience in Design patterns, ASP.NET, MVC, ANGULAR, ANGULAR JS, Windows application, WCF, ADO.NET, SQL Server and Test Driven Development (TDD) environment with JQuery, JavaScript, N-Unit, Entity Frameworks, LINQ, Code Refactoring and Business Objects Models.

User's Comments


 
Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID
Comment
 
 
 
 
 
 
Sponsors