【问题标题】:Why I am getting Multiple controller types were found that match the URL?为什么我得到多个与 URL 匹配的控制器类型?
【发布时间】:2021-12-31 17:20:56
【问题描述】:

我有一个名为 Shopex 的项目。

我改名为 HR

现在请求控制器

我收到以下错误

找到多个与 URL 匹配的控制器类型。如果多个控制器上的属性路由与请求的 URL 匹配,就会发生这种情况。

请求找到了以下匹配的控制器类型: HR.Web.Controllers.EmployeeController Shopex.Web.Controllers.EmployeeController

我变了

public static void ConfigureMvc(RouteCollection routes)
        {
            AreaRegistration.RegisterAllAreas();
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapMvcAttributeRoutes();
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Employee", action = "Index", id = UrlParameter.Optional },
                new[] { "HR.Web.Controllers" }
            );
        }

所以我添加了命名空间 HR.Web.Controllers

现在我需要在每个控制器类中将其作为属性进行吗

这是我的控制器

namespace HR.Web.Controllers
{
    [RoutePrefix("admin/employees")]
    public class EmployeeController : BaseController
    {
        private readonly IEmployeeService _employeeService;
        private readonly int _pageSize;

     
        public EmployeeController(IEmployeeService EmployeeService)
            : base()
        {
            _employeeService = EmployeeService;
            _pageSize = 10;
        }

        
        [Route("")]
        public async Task<ActionResult> Index(int pageIndex = 1, string q = "")
        {
            var employees = await GetEmployees(pageIndex, q, true);
            return View(employees);
        }

        [Route("unique")]
        public async Task<ActionResult> Unique(int pageIndex = 1, string q = "")
        {
            return Redirect("~/admin/Departments");

        }

        [Route("add")]
        public async Task<ActionResult> Add()
        {
            return View();
        }

        [Route("add")]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Add(Employee employee)
        {
            await _employeeService.AddAsync(employee);
            return RedirectToAction("Index");
        }





        [Route("edit/{id:int:min(1)}")]
        public async Task<ActionResult> Edit(int id)
        {
            var employee = await _employeeService.GetByIdAsync(id);
            if (employee == null)
            {
                return RedirectToAction("Index");
            }
            return View(employee);
        }


        [Route("edit/{id:int:min(1)}")]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit(Employee employee)
        {
            await _employeeService.UpdateAsync(employee);
            return RedirectToAction("Index");
        }





        [Route("delete/{id:int:min(1)}")]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Delete(int id)
        {
            var isAuthorized = await _employeeService.Delete(id);
            return RedirectToAction("Index");
        }
        private async Task<IList<Employee>> GetEmployees(int pageIndex, string keyword, bool shared)
        {
            if (pageIndex < 1)
            {
                pageIndex = 1;
            }
            var list = await _employeeService.GetAllAsync(pageIndex, _pageSize, keyword, shared);
            return list;
        }
    }
}

【问题讨论】:

  • 你有两次[Route("edit/{id:int:min(1)}")],是有意的吗?
  • @Rafalon 一个用于发布,一个用于获取
  • 是的,但你的帖子是Edit(Employee employee),它没有id作为参数
  • 我知道在项目名称为 shopex 之前它正在工作
  • 有些路由看起来像重复但没有动词属性。我认为它们不应该是原因,但是通过更改或删除它们很容易检查。如果某些东西是可疑的并且易于测试,我会从它开始。该错误还说明了多种控制器类型,但您只显示了一个控制器。您确定没有其他具有匹配路由的控制器吗?

标签: c#


【解决方案1】:

根据错误,

The request has found the following matching controller types: HR.Web.Controllers.EmployeeController Shopex.Web.Controllers.EmployeeController

这意味着您有多个EmployeeController。检查HR.Web.Controllers和Shopex.Web.Controllers中是否有多个控制器。如果没有,那么您编译的 dll 中可能有重复的控制器(检查 bin 文件夹)。

【讨论】:

    猜你喜欢
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 2020-02-05
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 2018-07-22
    相关资源
    最近更新 更多