0

I have a controller and want to define default route on it, like as follow:

public class SignInController : Controller
{
    [Route("", Name = "Default")]
    public ActionResult Index()
    {
        return View();
    }
}

on the RoutingConfig I comment out MapRoute

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

  /*routes.MapRoute("Default", "{controller}/{action}/{id}",
    new {controller = "SignIn", action = "Index", id = UrlParameter.Optional}
    );*/
}

When I start to server, I've got:

HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory.

What am I doing wrong?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
softshipper
  • 32,463
  • 51
  • 192
  • 400

1 Answers1

3

You doing attribute routing but are missing the configuration...

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

    routes.MapMvcAttributeRoutes();

    /*routes.MapRoute("Default", "{controller}/{action}/{id}",
         new {controller = "SignIn", action = "Index", id = UrlParameter.Optional}
      );*/
}    

...which will map the attribute-defined routes for the application.

Also if you are going to be using attribute routing you may want to set a route prefix for your controller.

[RoutePrefix("SignIn")]
public class SignInController : Controller
{
    //eg: GET signin/
    [Route("", Name = "Default")]
    public ActionResult Index()
    {
        return View();
    }
}

If you want the SignInController.Index to map to your root then set the RoutePrefix to "" (empty string)

Take a look at Attribute Routing in ASP.NET MVC 5

Nkosi
  • 235,767
  • 35
  • 427
  • 472