It’s been a little while since I made a new MVC project, so why not start at the end of the universe?
-
Start a new MVC project
-
Give it a model
12345678910111213using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace RestaurantsEndUniverse.Models{public class AboutMe{public string Name { get; set; }public string Title { get; set; }}} -
Pass the model to the View
12345678910111213141516171819202122232425262728293031using RestaurantsEndUniverse.Models;using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace RestaurantsEndUniverse.Controllers{public class HomeController : Controller{public ActionResult Index(){return View();}public ActionResult About(){ViewBag.Message = "Reviewing restaurants at the end of the universe.";var model = new AboutMe();model.Name = "Casey";model.Title = "Tech Lead";return View(model);}public ActionResult Contact(){return View();}}} -
Display model properties in the View
12345678@model RestaurantsEndUniverse.Models.AboutMe@{ViewBag.Title = "About Restaurants";}<h2>@ViewBag.Title.</h2><h3>@ViewBag.Message</h3><p>This site brought to you by @Model.Name, the @Model.Title.</p> -
Test that the model exists
12345678910111213141516171819202122232425262728using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web.Mvc;using Microsoft.VisualStudio.TestTools.UnitTesting;using RestaurantsEndUniverse;using RestaurantsEndUniverse.Controllers;namespace RestaurantsEndUniverse.Tests.Controllers{[TestClass]public class HomeControllerTest{[TestMethod]public void About(){// ArrangeHomeController controller = new HomeController();// ActViewResult result = controller.About() as ViewResult;// AssertAssert.IsNotNull(result.Model);}}} -
Apply fresh paint to Site.css
12345body {background-image: linear-gradient(45deg, white, grey);padding-top: 50px;padding-bottom: 20px;} -
Run it
-
How did we get here?
With the help of Global.asax and RouteConfig.cs
12345678910public class MvcApplication : System.Web.HttpApplication{protected void Application_Start(){AreaRegistration.RegisterAllAreas();FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);RouteConfig.RegisterRoutes(RouteTable.Routes);BundleConfig.RegisterBundles(BundleTable.Bundles);}}12345678910111213public class RouteConfig{public static void RegisterRoutes(RouteCollection routes){routes.IgnoreRoute("{resource}.axd/{*pathInfo}");routes.MapRoute(name: "Default",url: "{controller}/{action}/{id}",defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });}} -
Make a new Route with matching Controller
12routes.MapRoute("Styles", "styles/{name}",new { controller = "Styles", action = "Find", name = UrlParameter.Optional });1234567891011namespace RestaurantsEndUniverse.Controllers{public class StyleController : Controller{// GET: Stylepublic ActionResult Find(){return Content("Some style of food");}}}12> curl localhost:65006/Style/FindSome style of food