Follow this step to create new MVC Application on Microsoft Visual Studio 2010.
Creating MVC 4 Project
- File >> New Project
- Choose ASP.NET MVC4 Web Application. (If you dont have MVC 4, Install it here)
- On Project Template, choose Empty if you want to start from zero or choose Internet Application to start developement with default template.
- Choose View engine Razor.
- Click OK button
- Now you have create one project with MVC4 Solution
Adding First Controller
- Right click on the Controller folder under Solution Explorer Menu.
- Choose Add >> Controller
- Rename Controller Name to HomeController
- Since this is empty project, so under Scaffolding Options Template choose Empty MVC Controller
- Change The default ActionResult Index() like this
- Run the project, You should see the output on the browser like image below.
public string Index() { return "Hello World, This is my first MVC Application"; }
Understanding Route
As well as models, views, and controllers, MVC applications use the ASP.NET routing system, whichdecides how URLs map to particular controllers and actions. When Visual Studio creates the MVC project, it adds some default routes to get us started. You can request any of the following URLs, and they will be directed to the Index action on the HomeController:
- /
- /Home
- /Home.Index
So, when a browser requests http://yoursite/ or http://yoursite/Home, it gets back the output from
HomeController’s Index method. You can try this yourself by changing the URL in the browser. At the moment, it will be http://localhost:23859/, except that the port part may be different. If you append /Home or /Home/Index to the URL and hit return, you will see the same Hello World result from the MVC application.
This is a good example of benefiting from following MVC conventions. In this case, the convention is that we will have a controller called HomeController and that it will be the starting point for our MVC application. The default routes that Visual Studio creates for a new project assume that we will follow this convention. And since we did follow the convention, we got support for the URLs in the preceding list.
If we had not followed the convention, we would need to modify the routes to point to whatever
controller we had created instead. For this simple example, the default configuration is all we need.
Note : You can see and edit your routing configuration by opening the Global.asax.cs file
By Mohd Zulkamal
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)
0 comments:
Post a Comment