Mar*_*ark 1 c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4
我在添加新路由时遇到问题,允许我进行分页.
在我的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" }
);
}
}
Run Code Online (Sandbox Code Playgroud)
我的Global.asax.cs文件在Application_Start下有:
RouteConfig.RegisterRoutes(RouteTable.Routes);
Run Code Online (Sandbox Code Playgroud)
在我的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);
}
Run Code Online (Sandbox Code Playgroud)
但是当我导航到http://localhost:64296/offer/page/1- 我收到错误消息:
'/'应用程序中的服务器错误.
无法找到该资源.
说明:HTTP 404.您要查找的资源(或其中一个依赖项)可能已被删除,名称已更改或暂时不可用.请查看以下网址,确保拼写正确.
请求的URL:/ offer/page/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 }
);
Run Code Online (Sandbox Code Playgroud)
有关自定义路线的更多信息,请访问http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-custom-routes-cs
| 归档时间: |
|
| 查看次数: |
1203 次 |
| 最近记录: |