“Route”是“Microsoft.AspNetCore.Components.RouteAttribute”和“Microsoft.AspNetCore.Mvc.RouteAttribute”之间的不明确引用

Ali*_*taz 5 .net routes .net-core asp.net-core

我正在使用属性路由。我需要两个程序集参考。我已经尝试过,但对我不起作用。有谁知道如何解决这一问题?这是我的控制器代码:命名空间

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Task1CRUD.Repository;
using Task1CRUD.Model;

namespace Task1CRUD.Controllers
{

    [Route("api/[Controller]")]
    [ApiController]
    public class CustomerController:Controller
    {
            private readonly ICustomerRepository _customerRep;
            public CustomerController(ICustomerRepository customerRepository)
            {
                _customerRep = customerRepository;
            }

            // GET api/values
            [HttpGet("")]
            public async Task<List<Customer>> GetCustomers()
            {
                return await _customerRep.GetCustomers();
            }

    }
}
Run Code Online (Sandbox Code Playgroud)

小智 0

您可以直接在 RouteAttribute 上指定完整的类名,如下所示:

[Microsoft.AspNetCore.Mvc.Route("api/[Controller]")]
[ApiController]
public class CustomerController:Controller
{
        private readonly ICustomerRepository _customerRep;
        public CustomerController(ICustomerRepository customerRepository)
        {
            _customerRep = customerRepository;
        }

        // GET api/values
        [HttpGet("")]
        public async Task<List<Customer>> GetCustomers()
        {
            return await _customerRep.GetCustomers();
        }

}
Run Code Online (Sandbox Code Playgroud)

  • 我知道如何为命名空间添加别名。2021 年的 AspNet Core 5.0 现在使用哪个“一”?选择是“System.Web.Http.Route”、“Microsoft.AspNetCore.Components.Route”还是“Microsoft.AspNetCore.Components.Route”? (6认同)