In an ASP.NET MVC application, there is nothing like a page and it also doesn’t include anything that directly corresponds to a page when you specify a path in URL. The closest thing to a page in an ASP.NET MVC application is known as a View.
In ASP.NET MVC application, all incoming browser requests square measure handled by the controller and these requests square measure mapped to controller actions. A controller action may come a view or it’d additionally perform another style of action like redirecting to a different controller action.
Let’s take a glance at a straightforward example of view by making a brand new ASP.NET MVC project.
Step one − Open the Visual Studio and click on File → New → Project menu possibility.
Step two − From the left pane, choose Templates → Visual C# → internet.
Step three − within the middle pane, select ASP.NET internet Application.
Step four − Enter the project name ‘MVCViewDemo’ within the Name field and click on alright to continue. you may see the subsequent dialog that asks you to line the initial content for the ASP.NET project.
Step five − to stay things easy, choose the Empty possibility and check the MVC checkbox within the ‘Add folders and core references for’ section and click on Ok.
It will produce a basic MVC project with smallest predefined content. we have a tendency to currently have to be compelled to add controller.
Step Six − Right-click on the controller folder within the answer mortal and choose Add → Controller.
Step seven − choose the MVC five Controller – Empty possibility and click on ‘Add’ button.
Step Eight − Set the name to HomeController and click on ‘Add’ button.
You will see a brand new C# file ‘HomeController.cs’ within the Controllers folder that is open for piece of writing in Visual Studio further.
Let’s update the HomeController.cs file, that contains 2 action ways as shown within the following code.

using System;
using System.Collections.Generic;
using System.Linq;

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

namespace MVCViewDemo.Controllers {
   public class HomeController : Controller{
      // GET: Home
      public ActionResult Index(){
         return View();
      }
		
      public string DemoController(){
         return "Hi, I am a controller";
      }
   }
}

Step Nine − Run this application and apend /Home/DemoController to the universal resource locator within the browser and press enter. you may receive the subsequent output.

As DemoController action merely returns the string, to come a view from the action we want to feature a view initial.
Step Ten − Before adding a view let’s add another action, which is able to come a default view.

using System;
using System.Collections.Generic;
using System.Linq;

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

namespace MVCViewDemo.Controllers {
   public class HomeController : Controller{
      // GET: Home
      public ActionResult Index(){
         return View();
      }
		
      public string DemoController(){
         return "Hi, I am a controller";
      }
		
      public ActionResult ViewExample(){
         return View();
      }
   }
}

Step Eleven − Run this application and apend /Home/ViewExample to the universal resource locator within the browser and press enter. you may receive the subsequent output.
You can see here that we’ve a blunder and this error is truly quite descriptive, that tells United States of America it cannot notice the ViewExample view.
Step Twelve − to feature a view, right-click within the ViewExample action and choose Add view.
It will show the Add view dialog and it’s progressing to add the default name.

Step Thirteen − Uncheck the ‘Use a layout page’ checkbox and click on ‘Add’ button.
We currently have the default code within view.

Step Fourteen − Add some text during this view exploitation the subsequent code.

@{
   Layout = null;
}

<!DOCTYPE html>
<html>
   <head>
      <meta name = "viewport" content = "width = device-width" />
      <title>ViewExample</title>
   </head>
	
   <body>
      <div>
         Hi, I am a view
      </div>
   </body>
	
</html>

Step Fifteen − Run this application and apend /Home/ViewExample to the universal resource locator within the browser. Press enter and you may receive the subsequent output.

Leave a Reply

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