How to change Default Controller in MVC application?

Sourav.Kayal
Posted by in ASP.NET MVC category on for Beginner level | Points: 250 | Views : 2809 red flag

How to change default controller setting in MVC application.

How to change Default Controller in MVC application?

This is the first process which needs to learn to work with MVC application. When we start MVC application by default it throws page not found error like below.

 

 

In this article we will learn, how to solve this problem.  This article assumes that you already have very little experience of MVC and its various sections.

The error is due to not set proper controller. OK, now have a look on Global.aspx page of MVC application. We will find below section in Global.aspx page.

public static void RegisterRoutes(RouteCollection routes)
        {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
 
        } 

If observed closely, we will find there is MapRoute() function which contents Default controller and the controller name is Home. Now, if we go and visit controller folder of application, we will not find any Home Controller (By default in new MVC project) into it.

And when application starts, it tries to invoke Home controller when it is not available. And that’s why it throws error.

Solution:

We can solve this problem in two ways.

1)      By adding Home Controller

2)      By changing Default Controller in Global.aspx page.


Add Home Controller

Here we will just add one Home controller in controller section. Like below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace MVC3.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Call/
        public ActionResult Index()
        {
            return View();
        }
    }
}

And within Home controller, Index Action is present .So no problem. This is the first solution.


Change Default controller in Global.aspx page

To do same, at first we will add one controller like below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace MVC3.Controllers
{
    public class TestController : Controller
    {
        //
        // GET: /Call/
        public ActionResult MyTest()
        {
            return View();
        }
        public ActionResult Index()
        {
            return View();
        }
    }
}

We have added Test Controller and we would like to invoke MyTest() action by default. Now, we have to change default controller setting in Global.aspx page

 
public static void RegisterRoutes(RouteCollection routes)
        {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  routes.MapRoute(         "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Test", action = "MyTest", id = UrlParameter.Optional } // Parameter defaults
            );
 
       }

Now, the error has solved.


Conclusion:

Those are two possible ways to solve this problem. Hope you have understood them.

 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)