brianhevans
brianhevans

Reputation: 1193

ASP.NET WEBAPI add custom route

I have the new .NET WebAPI up and running and I am trying to add a new route to my project so that I can see the SettingsController in action. I have added my route to the Global.asax file within the RegisterRoutes method but I receive a 404 error when I try and browse to: sampleUrl:12345/settings/index. I am able to see: sampleUrl:12345/home/index without issue.

Global.asax

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

    routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );

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

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

Do I need to have an ActionResult Index() in my SettingsController?

Upvotes: 0

Views: 1014

Answers (1)

Michal Klouda
Michal Klouda

Reputation: 14521

Your settings route is redundant. What you are probably missing is the index method in settings controller.

Upvotes: 2

Related Questions