gpt4 book ai didi

c# - MVC4错误-路由表中没有路由匹配

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:53 26 4
gpt4 key购买 nike

我是 C# 和 MVC4 编程的新手,错误发生在

@{ 
Html.RenderAction("Menu", "Nav");
}

它返回一个错误,指出路由表中没有路由与提供的值匹配。我尝试在互联网上搜索无济于事,我们将不胜感激。谢谢

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
<link href="~/Content/Site.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="header">
<div class="title">Bag Labels</div>
</div>
<div id="categories">
@{ Html.RenderAction("Menu", "Nav"); }
</div>
<div id="content">
@RenderBody()
</div>
</body>
</html>

这是_Layout.cshtml

这是我的 NavController.cs

using NavisionStore.Domain.Abstract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NavisionStore.WebUI.Controllers
{
public class NavController : Controller
{
private IProductRepository repository;

public NavController(IProductRepository repo)
{
repository = repo;
}
public PartialViewResult Menu(string category = null)
{
ViewBag.SelectedCategory = category;

IEnumerable<string> categories = repository.Products
.Select(x => x.Category)
.Distinct()
.OrderBy(x => x);

return PartialView(categories);
}
}
}

这是我的 RouteConfig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

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

routes.MapRoute(null,
"",
new {
controller = "Product", action = "List",
category = (string)null, page = 1
}
);

routes.MapRoute(null,
"Page{page}",
new { controller = "Product", action = "List", category = (string)null },
new { page = @"\d+" }
);

routes.MapRoute(null,
"{category}",
new { controller = "Product", action = "List", page = 1 }
);

routes.MapRoute(null,
"{category}/Page{page}",
new { controller = "Product", action = "List" },
new { page = @"\d+" }
);

routes.MapRoute(null, "{contoller}/{action}");
}
}
}

这是我的 ProductController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NavisionStore.Domain.Abstract;
using NavisionStore.Domain.Entities;
using NavisionStore.WebUI.Models;



namespace NavisionStore.WebUI.Controllers
{
public class ProductController : Controller
{
private IProductRepository repository;
public int PageSize = 1;

public ProductController(IProductRepository productRepository)
{
this.repository = productRepository;
}

public ViewResult List(string category, int page = 1)
{
ProductsListViewModel ViewModel = new ProductsListViewModel
{
Products = repository.Products
.Where(p => category == null || p.Category == category)
.OrderBy(p => p.Id)
.Skip((page - 1) * PageSize)
.Take(PageSize),
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = repository.Products.Count()
},
CurrentCategory = category
};

return View(ViewModel);
}
}
}

最佳答案

从您的路由配置中删除以下路由:

routes.MapRoute(null,
"",
new {
controller = "Product", action = "List",
category = (string)null, page = 1
}
);


routes.MapRoute(null, "{contoller}/{action}");

此外,以下路由可能没有按照您的预期进行,因为每个路由实际上都为每个匹配的 url 返回相同的 Controller /操作。我建议删除它们。

routes.MapRoute(null,
"Page{page}",
new { controller = "Product", action = "List", category = (string)null },
new { page = @"\d+" }
);

routes.MapRoute(null,
"{category}",
new { controller = "Product", action = "List", page = 1 }
);

routes.MapRoute(null,
"{category}/Page{page}",
new { controller = "Product", action = "List" },
new { page = @"\d+" }
);

它们可以替换为以下内容:

routes.MapRoute(
"Page",
"Product/List/{category}/{page}",
new { controller = "Product", action = "List", page = UrlParameter.Optional },
new { page = @"\d*" }
);

最后,为所有路由添加名称,而不是null。目前,您唯一命名的是“默认”。

最后,对于您要尝试执行的操作,您应该使用更简单的路由配置:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

// matches:
// product/list/unicorns
// product/list/carrots/15
routes.MapRoute(
"Page",
"Product/List/{category}/{page}",
new { controller = "Product", action = "List", page = UrlParameter.Optional },
new { page = @"\d*" } //<-- note the asterisk here
);

routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new {controller = "Product", action = "List",
id = UrlParameter.Optional
}

关于c# - MVC4错误-路由表中没有路由匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19457970/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com