简体   繁体   English

Asp.net Mvc路由不起作用

[英]Asp.net Mvc Route not working

I have the following route on my mvc 5 project. 我在mvc 5项目上有以下路线。 When I try to access to the action by url, it throws an 404 error page. 当我尝试通过url访问操作时,它会引发404错误页面。 What am I doing wrong? 我究竟做错了什么?

RouteConfig.cs 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();
        }

    }

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 使用此定义,您可以使用请求url访问ListByEvent操作方法

yourSiteName/eventos/101/movimientos or yourSitename/EventMoneyMovements/ListByEvent?eventID=101 where 101 is any int value yourSiteName/eventos/101/movimientosyourSiteName/eventos/101/movimientos yourSitename/EventMoneyMovements/ListByEvent?eventID=101 ,其中101是任何int值

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. 您必须在默认MVC路由之前定义您的自定义路由,因为在路由配置中定义的所有路由都按照与在路由配置中定义的顺序相同的顺序维护在路由表中。

For more information refer below url: 有关更多信息,请参见下面的URL:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM