.Net Core 3.1 Web API Throwing 404 on new Controller
我已经在.net core 3.1的全新Web api项目中创建了一个新的控制器。每当我尝试发布到路线时,我都会得到404。
控制器设置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | [ApiController] [Route("[controller]")] public class AuthCodeController : Controller { private readonly ApplicationContext _context; public AuthCodeController(ApplicationContext context) { _context = context; } [HttpPost] [Consumes("application/x-www-form-urlencoded")] public JsonResult GetAuthCode(AuthCode authCode) { try { var theCodes = _context.AuthCodes.ToList(); var tmpCode = new Random(); var myNum = tmpCode.Next(100000, 999999999); while (theCodes.Any(tc => tc.AuthCodeRnd == myNum)) { myNum = tmpCode.Next(); } if (authCode.AuthCodeRnd > 0) { var removeCode = _context.AuthCodes.FirstOrDefault(c => c.AuthCodeRnd == authCode.AuthCodeRnd); if (removeCode != null) _context.AuthCodes.Remove(removeCode); } Guid authGuid = Guid.NewGuid(); var tmpRec = new AuthCode { Guid = authGuid, AuthCodeRnd = myNum, Address = authCode.tAddress, SmallLogoAddress = authCode.SmallLogoAddress, ClientFolder = authCode.ClientFolder, CompanyFolder = authCode.CompanyFolder, Folder = authCode.Folder }; _context.AuthCodes.Add(tmpRec); _context.SaveChanges(); var retVals = new AuthResponse { Guid = authGuid, ReturnAuthCode = myNum }; return Json(retVals); } catch (Exception ex) { var message = new ErrorResponse(); message.Status ="An Error Has Occured"; message.Message = ex.ToString(); return Json(message); } } } |
当我发布到此方法时,我收到404。我使用的网址是
我在startup.cs中所做的唯一修改是添加了dbcontext服务选项。其他所有内容均为默认设置。我可以显示天气预报。
编辑-添加了startup.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddControllers(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } |
已解决:我需要在POSTMan中禁用SSL验证
您应该设置属性: