我应该如何解决 ASP.NET 核心多端点错误
How should I resolve ASP.NET Core multiple endpoints error
我的控制器是:
[Route("api/[controller]")]
[ApiController]
public class SubjectController : ControllerBase
{
private TourActivityExpenseContext tourActivityExpenseContext = new TourActivityExpenseContext();
[HttpGet]
public IEnumerable<SubjectTo> Get()
{
var subjects = tourActivityExpenseContext.SubjectTos.ToList();
return subjects;
}
[HttpGet("{subject}")]
public int GetSubjectId(String subject)
{
var subjects = tourActivityExpenseContext.SubjectTos.ToList();
var isSub = subjects.FirstOrDefault(x => x.Subject == subject);
int id = isSub.Id;
return id;
}
[HttpGet("{id}")]
public string Get(int id)
{
var subjects = tourActivityExpenseContext.SubjectTos.ToList();
var isSub = subjects.FirstOrDefault(x => x.Id == id);
String sub = isSub.Subject;
return sub;
}
}
当在 Postman 中使用URL like
请求时
http://localhost:5000/api/Subject/Meeting
我收到这样的错误:
Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints. Matches:
TourExp.Controllers.SubjectController.Get (TourExp)
TourExp.Controllers.SubjectController.GetSubjectId (TourExp)
试着给他们一个具体的路线:
[HttpGet("GetSubject/{subject}")]
public int GetSubjectId(String subject)
{
var subjects = tourActivityExpenseContext.SubjectTos.ToList();
var isSub = subjects.FirstOrDefault(x => x.Subject == subject);
int id = isSub.Id;
return id;
}
我的控制器是:
[Route("api/[controller]")]
[ApiController]
public class SubjectController : ControllerBase
{
private TourActivityExpenseContext tourActivityExpenseContext = new TourActivityExpenseContext();
[HttpGet]
public IEnumerable<SubjectTo> Get()
{
var subjects = tourActivityExpenseContext.SubjectTos.ToList();
return subjects;
}
[HttpGet("{subject}")]
public int GetSubjectId(String subject)
{
var subjects = tourActivityExpenseContext.SubjectTos.ToList();
var isSub = subjects.FirstOrDefault(x => x.Subject == subject);
int id = isSub.Id;
return id;
}
[HttpGet("{id}")]
public string Get(int id)
{
var subjects = tourActivityExpenseContext.SubjectTos.ToList();
var isSub = subjects.FirstOrDefault(x => x.Id == id);
String sub = isSub.Subject;
return sub;
}
}
当在 Postman 中使用URL like
请求时http://localhost:5000/api/Subject/Meeting
我收到这样的错误:
Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints. Matches:
TourExp.Controllers.SubjectController.Get (TourExp)
TourExp.Controllers.SubjectController.GetSubjectId (TourExp)
试着给他们一个具体的路线:
[HttpGet("GetSubject/{subject}")]
public int GetSubjectId(String subject)
{
var subjects = tourActivityExpenseContext.SubjectTos.ToList();
var isSub = subjects.FirstOrDefault(x => x.Subject == subject);
int id = isSub.Id;
return id;
}