Phill Greggan
Phill Greggan

Reputation: 2394

why my new route Map for MVC does not work?

this app is out of the box mvc4 internet app from vs2012 templates. i have added a new route path for the url pattern sports/{name}

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

            routes.MapRoute(
                "Sports",
                "sports/{name}",
                new { controller="sports", action="Find",name=UrlParameter.Optional }
                );

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

the controller the home page is

public class sportsController : Controller
{
    //
    // GET: /sports/

    public ActionResult Find(string strName)
    {
        return Content("Sport looking for is: "+ strName);
    }

}

the page works and displays the text "Sport looking for is" if i request the default home page http:/localhost/sports. But if pass a parameter to the url such as "tennis" that parameter wont get passed to the action method Find().

here is the video of what happening, i want to know why this does not pass the parameter and how to fix this?

thanks

Upvotes: 0

Views: 903

Answers (1)

user3559349
user3559349

Reputation:

You route defines a parameter name = UrlParameter.Optional but the method your calling has a parameter string strName (they do not match). Change the method signature to

public ActionResult Find(string name)

Upvotes: 3

Related Questions