0
votes

I'm trying to create a static route as follows:

public static void RegisterRoutes(RouteCollection routes) {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
        name: "Hire",
        url: "Hire/{gender}",
        defaults: new { controller = "Hire", action = "Index" }
    );
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

however, when I try access /Hire/Female, for instance, IIS throws a 404 error, as I was reading about routing I noticed routes are usually written as {controller}/{action}/{something}, is it mandatory to have {controller} and {action} on my route?

here's the code for the controller:

public class HireController : Controller
{
    [HttpGet]
    public ActionResult Index(string gender)
    {
        return View();
    }
}

and I have a file named /Views/Hire/Index.cshtml

what I was trying to achieve is to route requests from /Hire/Male and /Hire/Female to the Index action of my HireController but I have the feeling I'm forgetting something since I've tried in different ways and always have a 404 returned


Update 1

I installed the RouteDebugger from http://www.codeproject.com/Articles/299531/Custom-routes-for-MVC-Application and it just shows the "basic" routes from MVC ({resource}.axd/{*pathInfo} and {controller}/{action}/{id}), it doesn't show the route I mapped

1
your routes are set up correctly. your are not need to provide controller and action name as part of the url when they are mention in the route values. are you by any means trying to accessing /Hire/Female using POST - Parv Sharma
@ParvSharma I'm typing the URL directly into the browser, I tried to remove the [HttpGet] as well but still no go - leandro koiti
make sure you dont have any folder named Hire in the root of the website - Parv Sharma
@ParvSharma thanks I've checked and the only folder named "Hire" is inside the "Controllers" folder, I saw a guy with a very similar problem in the related questions, he said he re-created the controller and everything worked, but that didn't work for me either, there is no configuration I have to add on web.config to "enable" routing right? - leandro koiti
yes. try recompiling the project - Parv Sharma

1 Answers

0
votes

It turns out I added the RegisterRoutes method in the wrong location, I thought I had to include that method inside Global.asax, I guess prior to MVC4 the Global.asax was the right location, however, on MVC4 I have to map the routes using the file located in /App_Start/RouteConfig.cs