0

I have the following route on my mvc 5 project. When I try to access to the action by url, it throws an 404 error page. What am I doing wrong?

RouteConfig.cs

  .... 
routes.MapRoute
       (
              name: "EventMoneyMovements2",
              url: "eventos/{eventID}/movimientos",
              defaults: new { controller = "EventMoneyMovements", action = "ListByEvent", eventID = UrlParameter.Optional }

       );

Controller

public class EventMoneyMovementsController : Controller
    {
        //
        // GET: /EventMoneyMovements/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ListByEvent(int? eventID)
        {

            return View();
        }

    }
2
  • 2
    Can you please post the exact url you are trying to goto... Commented Jan 25, 2016 at 3:47
  • You're going to need to post your whole RouteConfig. Order matters. If the default route is at the top with {controller}/{action}/{id} it will always be the one that's routed. Commented Jan 25, 2016 at 3:55

2 Answers 2

1

The order in which you are registering the routes matters. You should always register your specific routes before the generic default route.

Assuming you register your specific route before your generic like below.

public static void RegisterRoutes(RouteCollection routes)
{
   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
   routes.MapRoute
   (
          name: "EventMoneyMovements2",
          url: "eventos/{eventID}/movimientos",
          defaults: new { controller = "EventMoneyMovements", action = "ListByEvent",
                                                         eventID = UrlParameter.Optional }

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

With this definition you can access the ListByEvent action method with the request url

yourSiteName/eventos/101/movimientos or yourSitename/EventMoneyMovements/ListByEvent?eventID=101 where 101 is any int value

Sign up to request clarification or add additional context in comments.

Comments

1

You must define your custom route before default MVC route because all routes defined in route config maintained in a route table in same order as we define in route config.

For more information refer below url:

http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/creating-custom-routes-cs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.