In this tutorial we will learn how to create a MVC
project.
Asp.net MVC is basically an architectural pattern , which allow us to create a dynamic
website using HTML. This gives feature of "TDD development".
Following some
steps for creating your first Asp.net MVC projects.
1.
Open visual studio and click on
2.
File -> New -> Project
3.
Here you will see a screen from here select Web and ASP.NET MVC4 Web Application and give
the name for this project and click OK.
4.
When we will click ok we will see Asp.Net New MVC project screen from here select “Empty template “ and select Razor
as ViewEngine .
Razor And ASPX
are type of ViewEngine.
Here we will prefer “VIEWeNGINE”
because it is much clean and better in use.
5.
After click on OK ,It will create a MVC project. In Solution explorer we will see “Modal”, “view” , “controller”
folder .
6.
Now go to controller folder and add a new
controller to the project .
Right click on Controllers folder -> Select Add -> Controller
Give the name for this controller As FirstController and click on Add and it will generate a file with the name FirstController .
Here when you will click on add , this
will add a class “FirstController.cs” class. it will be available in controller folder. This class will be inherited by “Controller” Base class
Here this class will contain a method
and the return type of this method is “ActionResult”
7. Add ActionMethod To the Controller
class
ActionMethods are used for handling “user
request” , validation of user input, execute code and the o/p.
MVC routing engine send the user
request to the appropriate ActionMethod.
Now open “FirstController.cs” Class
and add a ActionMethod and the give name it as “Index”
The following code which is introduce in
FirstController.cs Class
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace
FirstMvcApplication.Controllers
{
public
class FirstController
: Controller
{
//
// GET: /Home/
public string
Index()
{
return "Hello,
from first MVC application";
}
}
}
|
There is a function “index” which will
return String , now at this point if you will run this application it will deploy
at local server and will give the output.
8.
Here in this condition if you will run this
application it will give error.it is
because if you will go to
App_Start
-> RouteConfig.cs
class there is code which contain first which method and which
controller class to be execute.
Here by default controller name is “Home”
but we don’t have controller name with this name. So for execution this change
the name of controller from Home to
First.
Change
this Home Controller to First controller For this code is
below
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace
FirstMvcApplication
{
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 = "First",
action = "Index", id = UrlParameter.Optional }
);
}
}
}
|
9. Now
Run your application it will output as "Hello, from first MVC
application"
10.
At
this time this application is running on local sever for deploying this
application IIS sever there are some steps
Go to start -> press Run - > in
Run window type “InetManager”
Or
Go to Control panel -> Administrative
Tools - > search for Internet
Information Services (IIS) Manager
Here if you are not able to see Internet
Information Services (IIS) Manager then install it in your system. For this go
to How To install IIS Server
11.
On
IIS server screen you are not able to see your application (FirstMVCApplication
) so for this go back to you application
and right click on your application and go to properties click on WEB you will see there that you are
using visual studio development server but instead of that we want to use local
IIS web server so for this check
redio button.
You remember that for creating you application on IIS server you
have to open VS2010 as “Run as Administrator”.
Now click on Create virtual directory
12.
After creating virtual directory again go to
IIS server refresh it then you will see there your application. It means now
your application will run on IIS server.
13.
Now
when you will run this application you will see the URL that itis running on “localHost/FirstMVCApplication/”
You
will see the output “"Hello, from first MVC application"”
Very nice post Mr.Munesh keep post more on MVC.
ReplyDeleteThank you ..
DeleteNice Article.. I really Loved
ReplyDelete