Learn How to Create ASP.NET MVC Application in Visual Studio

Introduction

In this article, we are going to build an ASP.NET MVC Application and understand how MVC request works. let’s go and create an MVC project. then, open Visual Studio and click on File > New Project.

build an ASP.NET MVC Application

And we are going to create an ASP.NET MVC 4 web application. and let’s call this MVCDemo, then select web form Empty template.

Select View Engine

In Dropdownlist, we get two options. here meaning we have to build in view engine Aspx and Razor view. then we use the Razor view engine because it has got much cleaner code. for now, let’s select the razor as a view engine.

select razor is view engine.

Project Structure

And then create an MVC project for us. now, let go to another instance of Visual Studio that’s an application running on the machine. and Let’s create an ASP.NET MVC web application and now we have an MVC demo project.

And then give your controller a meaningful name. we want to call this Home controller. and that should add a file with the name HomeController.cs to the controller’s folder. and this nothing but a class file.

Controllers and Views

And notices within the Solution Explorer. we have model views and controllers. you know this folder, these the name suggests. they’re going to contain model views and controllers of our ASP.NET MVC application. now let’s go ahead and add a controller to our MVC project. and to the right click, the controller folder adds a controller.

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

Then it has got this class with the name Home controller. and notice this whole class actually inherits from the Base Controller class. and classes can contain the function. so this controller class has got one function at the moment which is an index. and locates the return type is returning something called ActionResult. the function return string instead of ActionResult. and what string do we want to return a message from the MVC application saying “Hello from MVC application“.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace MVCDemo.Controllers
{
    public class HomeController : Controller
    {
        
        // GET: /Home/
        public string Index()
        {
            return "Hello from MVC Application";
        }
    }
}

In ASP.NET MVC application URL bind to controller and controller action methods. The MVC controllers folder by default is there a controller. with name home and the function within the controller. so, the HomeController and index function invoke this URL is going to invoke the index function. that resides in Home controllers.

asp.net mvc output

We know the ASP.NET MVC application present in the controller’s folder. by default, in MVC URLs are the map to controller action methods. of usually functions inside a controller. that can respond to URLs are call as controller action methods. and the webforms URL mapped to the physical file.

In the MVC application, we have a Home controller and within the index function. we get this message back. instead of the index function saying something like “Hello from MVC Application“.

Routing

The Index action method within the Home controller class. then how is the URL mapped to the create controller action method. let’s understand and go to Visual Studio.

routeconfig url mapping

The name of the virtual directory is MVCDemo. we don’t have the controller name and the action method name. but still, the application successfully invoked the index function. within, the HomeController class. so, if you look at Global.asax. notice that we have this event handler method application_start. this event gets executes when the application first starts.

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

The register route method is present in Route_config class. and will look at the folder App_start. it has got this class Route_config. if actually right-click on that RegisterRoute. and then go to definition RouteConfig class. which is defined within RouteConfig.cs which is present in the App_Start folder

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
            }
        );
    }
}

And within this class, we have this RegisterRoute method. so, pay attention to right here the name it is the default meaning this is the default route, and look at the URL pattern.

After call index function to execute. if we don’t pass anything and then press enter. obviously, it works because we have default specified route.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace MVCDemo.Controllers
{
    public class HomeController : Controller
    {
        public string Index(string id)
        {
            return "ID = "+ id + Request.QueryString["name"];
        }
    }
}

ASP.NET MVC will pass any query string to form post parameters name. the name two index action method when this URL. that you see URL, that’s how you are printing those values. you can use the list of query string Request object query string property. and index route function within this RouteConfig class.

Thanks for the Reading article! I hope this get’s you started app using ASP.NET MVC. Take care.

Leave a Reply

Your email address will not be published. Required fields are marked *