while i was working on a project where MVC architecture i was completely new to it . i had to refer many e learning materials and finally i started creating an application myself for learning purpose .
the best thing i did was installing VS 2013 ultimate edition .
if you click on New Projects -Web you find the below screen .
Select MVC option from Icons available and click OK
and you will see that your solution is ready with some predefined folder in the solution explorer
The first thing i did was Creating a Controller and Named it as HomeController. this is your controller class . then i created an ActionResult to perform action and navigate to specific View.
namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
public ActionResult GotoHome()
{
return View("MyHomePage");
}
}
}
but then , where is the MyHomePage view ?? i had to create a View as well
and when you create View , it is actually .cshtml page and not a class
placed a simple sentence in the body of the page
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>MyHomePage</title>
</head>
<body>
<div>
Welcome to my first MVC View
</div>
</body>
</html>
and now ran the solution . but i endedup with some error . i forgot to include controller name and action name in the url and by doing that only i could navigate to desired page
but then this is complicated and wrong way of doing. user will never remember such things
so i had to route is little dynamically . so i went to App_Start-> RouteConfig.cs and placed my navigation details like below
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Home",
url: "Home",
defaults: new { controller = "Home", action = "GotoHome", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Home1",
url: "",
defaults: new { controller = "Home", action = "GotoHome", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Home2",
url: "Home/Home",
defaults: new { controller = "Home", action = "GotoHome", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Now See the Home1 route Name. if i dont specify anything in the URL by default it routes to mentioned page where i mentioned the action
Click on f5 and see what you get in the browser
Simple isnt it ?? that's how i started learning MVC .i will keep posting new R&Ds on MVC
the best thing i did was installing VS 2013 ultimate edition .
if you click on New Projects -Web you find the below screen .
Select MVC option from Icons available and click OK
and you will see that your solution is ready with some predefined folder in the solution explorer
The first thing i did was Creating a Controller and Named it as HomeController. this is your controller class . then i created an ActionResult to perform action and navigate to specific View.
namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
public ActionResult GotoHome()
{
return View("MyHomePage");
}
}
}
but then , where is the MyHomePage view ?? i had to create a View as well
and when you create View , it is actually .cshtml page and not a class
placed a simple sentence in the body of the page
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>MyHomePage</title>
</head>
<body>
<div>
Welcome to my first MVC View
</div>
</body>
</html>
and now ran the solution . but i endedup with some error . i forgot to include controller name and action name in the url and by doing that only i could navigate to desired page
but then this is complicated and wrong way of doing. user will never remember such things
so i had to route is little dynamically . so i went to App_Start-> RouteConfig.cs and placed my navigation details like below
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Home",
url: "Home",
defaults: new { controller = "Home", action = "GotoHome", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Home1",
url: "",
defaults: new { controller = "Home", action = "GotoHome", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Home2",
url: "Home/Home",
defaults: new { controller = "Home", action = "GotoHome", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Now See the Home1 route Name. if i dont specify anything in the URL by default it routes to mentioned page where i mentioned the action
Click on f5 and see what you get in the browser
Simple isnt it ?? that's how i started learning MVC .i will keep posting new R&Ds on MVC
awsome
ReplyDelete