C# 无法使用GET访问API终结点
我正在努力构建一个API来处理.NETCore中的身份信息,但每次我尝试调用时,都会得到一个404 当我四处寻找答案时,似乎什么都不清楚,因为发布的代码似乎非常少。以下是所有我认为相关的东西 控制员:C# 无法使用GET访问API终结点,c#,.net-core,asp.net-core-mvc,asp.net-core-webapi,C#,.net Core,Asp.net Core Mvc,Asp.net Core Webapi,我正在努力构建一个API来处理.NETCore中的身份信息,但每次我尝试调用时,都会得到一个404 当我四处寻找答案时,似乎什么都不清楚,因为发布的代码似乎非常少。以下是所有我认为相关的东西 控制员: using Common.Extensions; using Identity.Database.Contexts.Models; using Identity.WebApi.Models; using Identity.WebApi.Models.Tokens; using Identity.W
using Common.Extensions;
using Identity.Database.Contexts.Models;
using Identity.WebApi.Models;
using Identity.WebApi.Models.Tokens;
using Identity.WebApi.Services.Access;
using Identity.WebApi.Services.Accounts;
using Identity.WebApi.Services.Tokens;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using Controller = Microsoft.AspNetCore.Mvc.Controller;
using Get = Microsoft.AspNetCore.Mvc.HttpGetAttribute;
using Post = Microsoft.AspNetCore.Mvc.HttpPostAttribute;
using Route = Microsoft.AspNetCore.Mvc.RouteAttribute;
namespace Identity.WebApi.Controllers
{
[Route("api/[controller]")]
public class IdentityController : Controller
{
private readonly IApplicationUserService _userService;
private readonly IAccessService _accessService;
private readonly ITokenService _tokenService;
private readonly SignInManager<ApplicationUser> _signInManager;
public IdentityController(IApplicationUserService userService, IAccessService accessService, ITokenService tokenService, SignInManager<ApplicationUser> signInManager)
{
_userService = userService;
_accessService = accessService;
_tokenService = tokenService;
_signInManager = signInManager;
}
[Get]
[AllowAnonymous]
public string Index()
{
return new Dictionary<string,string>
{
{ "status", "live" }
}.Serialize();
}
[Post]
[Route("create")]
[AllowAnonymous]
public Task<ISet<IdentityResult>> Create(string user)
{
var decodedUser = DecodeUser(user);
var applicationUser = new ApplicationUser(new User
{
Id = Guid.NewGuid(),
Name = decodedUser.Username,
LastActive = DateTime.UtcNow
});
return _userService.Add(applicationUser, decodedUser.Password);
}
private (string Username, string Password) DecodeUser(string encodedUser)
{
var decodedUser = encodedUser.DecodeFrom64().Split(':');
return (Username: decodedUser[0], Password: decodedUser[1]);
}
private async Task<bool> CheckPasswordAsync(ApplicationUser user, string password)
=> await _signInManager.UserManager.CheckPasswordAsync(user, password);
}
}
如果使用属性路由,则没有
api/identity/index
,因为示例中带有路由前缀的[HttpGet]
或Get
与
[Get] //<-- Matches GET api/identity
[AllowAnonymous]
public IActionResult Index() {
var result = new Dictionary<string,string>
{
{ "status", "live" }
}.Serialize();
return Ok(result);
}
参考控制器顶部的以下内容:
[Route("api/[controller]")]
public class IdentityController : Controller
这意味着,如果您的路由仅从api/
开始,那么它将与控制器匹配。此外,索引操作上没有任何额外的路由属性,因此它只查找api/identity
。但是,您的启动设置与该部分不匹配,并且由于您没有任何其他与之匹配的路由,因此您将获得404
由于这个原因,app.UseMvc
中的默认路由将不起作用
简单修复:在您的启动设置中将
launchUrl
更改为api/identity
。。。然后跟随@Nkosi的你能把返回404的路线包括进去吗?哦。。。谢谢@Nkosi,我错过了。我想你是对的-启动设置只需要更新到api/identity
是的,这一次让我很失望。现在,我通过控制器看到了我预期的依赖项注入错误(第一次在.NETCore中这样做)。谢谢
[Get] //<-- Matches GET api/identity
[AllowAnonymous]
public IActionResult Index() {
var result = new Dictionary<string,string>
{
{ "status", "live" }
}.Serialize();
return Ok(result);
}
[Post("create")] //<-- Matches POST api/identity/create
[AllowAnonymous]
public async Task<IActionResult> Create(string user) {
var decodedUser = DecodeUser(user);
var applicationUser = new ApplicationUser(new User
{
Id = Guid.NewGuid(),
Name = decodedUser.Username,
LastActive = DateTime.UtcNow
});
ISet<IdentityResult> result = await _userService.Add(applicationUser, decodedUser.Password);
return Ok(result);
}
[Route("api/[controller]")]
public class IdentityController : Controller