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

标签 c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing

我是 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/

相关文章:

c# - 为什么在 C# 中有些迭代器比其他迭代器快?

C#进程终止

c# - 构造匿名类型和嵌套循环的通用列表

Javascript - Json 还是 html?

asp.net - 如何在 ASP.NET MVC4 项目之间共享 HTML

c# - 从 Asp.Net MVC 中的 Html 内容生成 JPEG

c# - MVC 4路由找不到 Controller

C#多线程并发机制等待给定结果

asp.net-mvc - "' Sys ' is undefined"在 IIS7 中运行 ASP.NET MVC 应用程序时出错

c# - MVC 中的 P3P header 信息