MVC 4 - Pt 3 : Playing with Actions and Controllers

So we learned in the previous post that we can set up different routes in the RouteConfig.cs file, like so:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            // /Cuisine/french
            routes.MapRoute("Cuisine",
                "cuisine/{name}",
                new { controller = "Cuisine", action = "Search", name = UrlParameter.Optional });

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

now lets add another route and call it "cuisine" with an action of "Search". We need to add this route above the default route because the engine checks the code in sequence, if it finds /home first (which it pretty much always does) it won't recognize our newly added route to "cuisine". So let's add our Route Map for cuisine above the default Route Map.

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            // /Cuisine/french
            routes.MapRoute("Cuisine",
                "cuisine/{name}",
                new { controller = "Cuisine", action = "Search", name = UrlParameter.Optional });

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

 

Now, save and run your application, you will end up at the home page. Type cuisine/swedish after localhost like so, http://localhost:56470/cuisine/swedish. Then hit return. You'll get a 404 error, File Not Found. That's because we haven't created a cuisine controller or a search action for the routing engine to find. Let's do that now.

Right click on the Controllers folder and click Add > Controller. Add an empty MVC controller and call it CuisineController. Then, change the name of the Index action method to Search(), like this:

public class CuisineController : Controller
    {
        // GET: Cuisine
        public ActionResult Search(string name)

        {
            var message = Server.HtmlEncode(name);
            return Content(message);
        }
    }

Then, return Content and any string. This will prove to us that the routing engine is getting to the controller and performing the Search action method. Save and run your application then type the above URL into the browser. You should see your string displayed in the upper left corner of the browser. We did it! We created our cuisine controller! Fun, huh? Just like magic! 

You can even add a query string parameter and the MVC framework will find it, such as 

http://localhost:50688/cuisine?name=french

and it will spit out "french" in the browser window.

Add comment

Loading