【问题标题】:ASP.Net MVC Adding a Pager but route can't be foundASP.Net MVC 添加寻呼机但找不到路由
【发布时间】:2013-07-16 22:55:31
【问题描述】:

我在添加新路由以允许我进行寻呼时遇到问题。

在我的 Route.Config.cs 文件中,我添加了一条新路由,UpcomingOffers

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

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

        routes.MapRoute(
           name: "UpcomingOffers",
           url: "Offer/Page/{page}",
           defaults: new { controller = "Offer", action = "Index" }
       );
    }
}

我的 Global.asax.cs 文件在 Application_Start 下有:

 RouteConfig.RegisterRoutes(RouteTable.Routes);

在我的 Offer 控制器中,我有:

    //
    // GET: /Offer/
    // GET: /Offer/Page/2

    public ActionResult Index(int? page)
    {
        const int pageSize = 10;
        var item = db.Customers.OrderByDescending(x => x.OfferCreatedOn).ToList();
        var paginatedItems = item.Skip((page ?? 0) * pageSize)
            .Take(pageSize)
            .ToList();
        return View(paginatedItems);
    }

但是当我导航到http://localhost:64296/offer/page/1 - 我收到错误消息:

“/”应用程序中的服务器错误。

找不到资源。

描述:HTTP 404。您正在寻找的资源(或其之一 依赖项)可能已被删除,名称已更改,或者是 暂时不可用。请查看以下 URL 并制作 确保拼写正确。

请求的网址:/offer/page/1

谁能看到我做错了什么?我怀疑它在我的路线某处......

谢谢,

标记

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


    【解决方案1】:

    交换你的 2 条路线。添加到路由表的路由顺序很重要。新的自定义路由添加在现有的默认路由之前。如果你颠倒了顺序,那么总是会调用默认路由而不是自定义路由。

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

    更多关于自定义路线的信息在这里http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-custom-routes-cs

    【讨论】:

    • 谢谢@Darren - 只要 StackOverflow 允许(10 分钟),我就会标记为答案 - 当你知道怎么做时很容易!!!干杯,马克
    • 别担心,不客气。几周前我遇到了同样的问题:)
    • 简单好听的答案
    【解决方案2】:

    MVC 使用与 URL 匹配的第一个有效根。 您的 URL 与第一个粗糙的 {controller}/{action}/{id} 匹配。这就是它尝试查找 Controler=Offer 和 Action=Page 的原因。

    只需在您的 global.asax 中交换您的根注册

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-16
      • 1970-01-01
      • 1970-01-01
      • 2020-03-27
      • 1970-01-01
      • 2015-03-17
      • 2011-06-13
      • 2023-04-04
      相关资源
      最近更新 更多