29.05.2015 Views

o_19mgorv9t13a3ko71fev19l81mqa.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

public class HomeController : Controller {<br />

}<br />

}<br />

public ActionResult Index() {<br />

ViewBag.Controller = "Home";<br />

ViewBag.Action = "Index";<br />

return View("ActionName");<br />

}<br />

Create a Customer controller and set its contents to match Listing 15-2.<br />

Listing 15-2. The Contents of the CustomerController.cs File<br />

using System.Web.Mvc;<br />

namespace UrlsAndRoutes.Controllers {<br />

public class CustomerController : Controller {<br />

public ActionResult Index() {<br />

ViewBag.Controller = "Customer";<br />

ViewBag.Action = "Index";<br />

return View("ActionName");<br />

}<br />

}<br />

}<br />

public ActionResult List() {<br />

ViewBag.Controller = "Customer";<br />

ViewBag.Action = "List";<br />

return View("ActionName");<br />

}<br />

Create an Admin controller and edit its contents to match the code shown in Listing 15-3.<br />

Listing 15-3. The Contents of the AdminController.cs File<br />

using System.Web.Mvc;<br />

namespace UrlsAndRoutes.Controllers {<br />

public class AdminController : Controller {<br />

}<br />

}<br />

public ActionResult Index() {<br />

ViewBag.Controller = "Admin";<br />

ViewBag.Action = "Index";<br />

return View("ActionName");<br />

}<br />

Creating the View<br />

I specified the ActionName view in all of the action methods in these controllers, which allows me to define one view and<br />

use it throughout the example application. Create a folder called Shared in the Views folder and add a new view called<br />

ActionName.cshtml to it, setting the contents of the view to match Listing 15-4.<br />

362

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!