Tuesday, 30 June 2015

How can we restrict MVC (.Net) actions using GET or POST?

                    We can decorate the MVC action with the HttpGet or HttpPost attribute to restrict the type of HTTP calls.

                    For example you can see in the below code snippet the Home action can only be invoked by HttpGet. If we try to make HTTP POST on Home, it will throw an error.

//GET

      [HttpGet]
      public ActionResult Home()
     {
    
           return View();
      }

//POST

      [HttpPost]
      public ActionResult Home(Int Id)
     {
    
          return View(Id);
      }

What is routing in MVC (.Net)?

Routing helps you to define a URL structure and map the URL with the controller.

         For instance lets take one example. My Controller name is Static and Method name is Whoweare. So we want to display in URL like "http://localhost/Static/who-we-are/", Below is the code that you show of how to map that URL with controller and action.

routes.MapRoute(
               name: "Whoweare",
               url: "who-we-are",
               defaults: new { controller = "Static", action = "Whoweare", id = UrlParameter.Optional }
           );

What are HTML helpers in MVC (.Net)?

           
               HTML helpers help you to render HTML controls in the view. For instance if you want to display a HTML textbox on the view , below is the HTML helper code.

                                            "   <%= Html.TextBox("UserName") %>   "

                                            "   <%= Html.CheckBox("UserName") %> "

What are the benefits of using MVC (.Net)?

Benefits of using Model View Controller:

                  1. Separation of concerns is achieved as we are moving the code-behind to a separate class file. By moving the binding code to a separate class file we can reuse the code to a great extent.

                  2. Automated UI testing is possible because now the behind code has moved to a simple .NET class. This gives us opportunity to write unit tests and automate manual testing.

What is MVC in .Net?

             
                    MVC is an architectural pattern which separates the representation and user interaction. It is divided into three sections, Model, View, and Controller. 


  1. The View is responsible for the look and feel.
  2. Model represents the real world object and provides data to the View.
  3. The Controller is responsible for taking the end user request and loading the appropriate Model and View.