0

In MVC5, how can I use a "folder" as parameter for Index?

I have this controller method:

public class HookController : Controller
{
    [HttpGet]
    [Route("hook/{pattern}")]
    public ActionResult Index(string pattern)
    {      
    }
}

I can currently call it like this: /hook/?pattern=testpattern ..but I'd like pattern to be populated when I call /hook/testpattern as a folder/path.

I thought I could do it with [Route("hook/{pattern}")] but that has no effect and I just get 404 not found.

NickG
  • 9,315
  • 16
  • 75
  • 115
  • *I can currently call it like this: `/hook/?pattern=testpattern`* - For the record, that URL does not match `[Route("hook/{pattern}")]`. It is probably matching your default route instead. Routing *ignores* query string parameters, but they end up being passed into the model binder and action method parameters by [value providers](https://stackoverflow.com/a/36606015). – NightOwl888 Apr 05 '18 at 12:38

1 Answers1

0

Fixed it. MVC Routing Attributes weren't enabled as I was missing this line:

routes.MapMvcAttributeRoutes();

This goes in RegisterRoutes() (RouteConfig.cs) before the default route.

NickG
  • 9,315
  • 16
  • 75
  • 115