user603007
user603007

Reputation: 11794

Custom route does not work in ASP.net MVC 3

I am trying to implement my custom route in ASP.net MVC 3 but I get this error:

The resource cannot be found. 

global.asax

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

    routes.MapRoute(
        "mycontroller", // Route name
         "{controller}/{name}", // URL with parameters
         new { controller = "MyController", action = "Search" } // Parameter defaults
    );

}

MyController.cs

public class MyController : Controller
{
    public ActionResult Search(string name)
    {
        return Content(name);
    }
}

Upvotes: 2

Views: 689

Answers (2)

alok_dida
alok_dida

Reputation: 1733

Try changing it to

routes.MapRoute(
 "mycontroller", // Route name
 "{controller}/{name}", // URL with parameters
 new { controller = "My", action = "Search", } // Parameter defaults
);

Please register it in Global.asax file

Upvotes: 0

MisterJames
MisterJames

Reputation: 3326

Try this instead:

routes.MapRoute(
 "mycontroller", // Route name
 "mycontroller/{name}", // URL with parameters
 new { controller = "My", action = "Search", } // Parameter defaults
);

MyController won't be found because you don't have a controller named MyControllerController. By virtue of inheriting from Controller the convention will be looking for the URL token + "Controller".

Cheers.

Upvotes: 1

Related Questions