MVC

How to Enable Attribute Routing in MVC

Before going further lets know about what is Attribute Routing ? In MVC 5 this new feature was added and before that we used Convention Routing which looks like this

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

now imagine you are working on a e-commerce website and you have to define alot of routs for each action in RouteConfig, well you can do it but it will be difficult to debug and its not a clean way thats why Attribute Routing was introduced which gives developers great flexibility and ease.

Enabling Attribute Routing

Its really easy to enable attribute routing, all you have to do is just add

routes.MapMvcAttributeRoutes();

In the RouteConfig file like this

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

and thats it you have enabled the attribute routing.

Using Attribute Routing

unlike Convention Routing its really easy to use Attribute Routing, for route to work you have to define a custom route in [Route()] Attribute.

public class HomeController : Controller  
{
    [Route("~/Home/Index")]
    public ActionResult Index() { ... }
}

the above route will take you to URL  domainname/home/index

What is ASP.NET MVC

 

 

MVC means Model View Controller. MVC is actually an architecture which separates the web application into these components

1) Model

2) View

3) Controller

but there is another component called Routing which also servers an important role in MVC architecture but most of the time its not well explained i will try my best to explain each component.

1) Model's :

                    Model's is basically representation of database tables specially in Entity Framework because in every tutorial you will see implementation of entity framework with MVC so don't confuse yourself, you can also use Ado .Net, Drapper etc
                    These are framework's which helps the application to interact with database.
 

2) View's :

                      View's are the web pages which will be displayed upon request of the user. Views are actually UI (user interface) which include text,  text boxes, buttons etc and are rendered form of HTML.


3) Controller's :

                      Controller is the main component which handles user interactions with database or displays means what data does the user wants from database and what page does the user wants to display everything is handled by the controller.

 

4) Routing :

                     Routing also plays and important role in MVC. The main purpose of the router is to forward the user request to specific controller so when ever user makes a request it first goes to route then the route decides to which controller should it send  
                     the request.
 

 

Overall Flow :


                            As you can see in above picture first user makes a request like www.abc.com  it will first go to Router then router will pass the request to specific Controller according to the address. Controller will fetch the data from the database if 
                            needed and it will send the Model (object which will contain the data) to the View which will render the the data with HTML which will be displayed in the user browser.

Creating your First MVC Application

Here are some steps to create your first MVC Application

 

  1. First of all click on Visual Studio ( i am currently using VS 2017 )

                                
     
  2. Click on Create new project...
                
  3. A windows will be displayed select installed  => Templates  => Visual C#   => Web   =>  ASP. Net Web Application   => Give Name to your project



     
  4. A new window will be opened of Templates. In templates select MVC and below MVC will be selected in Add folders and core references for :


     
  5.  in above picture you will also see Empty in Templates, you can select this option if you want to start project from scratch select Empty and Check Mvc In
    Add folders and core references for : . The main difference between these two is if you select MVC from templete it will give you a simple project which
    will include a website.



     
  6. If you choose Empty there will be no website or login functionality it will have noting just empty folders



     
  7.   I have selected MVC template. So when you click ok it will show

            
     
  8. and finally when everything will be ready it will show some folders in solution explorer

                    

    I will explain later what is the purpose of each folder here.