c# - ASP.NET Core 3.1 Web API 发布方法导致 405“方法不允许”错误
问题描述
新手第一次尝试创建 ASP.NET Core 3.1 Web API。
我的GET
方法运行良好,但每当我尝试该POST
方法时,我都会收到 405“不允许的方法”错误。我搜索和搜索,大多数解决方案似乎都集中在禁用 webDAV 是web.config
文件或使用正确的路由路径。
我现在在本地运行,试图直接从 Visual Studio 2019 中测试出来,所以不web.config
存在。我正在POST
使用 Postman 向 URL 提交请求,http://localhost:58322/api/test/
其中端口 58322 是运行项目时分配的端口。
有人可以告诉我我做错了什么吗?
这是我的代码TestController.cs
:
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SimpleAPI.Models;
namespace SimpleAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
private readonly appDBContext _context;
public TestController(appDBContext context)
{
_context = context;
}
// GET: api/Test
[HttpGet]
public async Task<ActionResult<IEnumerable<Test>>> GetTest()
{
return await _context.Test.ToListAsync();
}
// GET: api/Test/5
[HttpGet("{id}")]
public async Task<ActionResult<Test>> GetTest(int id)
{
var test = await _context.Test.FindAsync(id);
if (test == null)
{
return NotFound();
}
return test;
}
// PUT: api/Test/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
[HttpPut("{id}")]
public async Task<IActionResult> PutTest(int id, Test test)
{
if (id != test.DataId)
{
return BadRequest();
}
_context.Entry(test).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!TestExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Test
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
[HttpPost]
public async Task<ActionResult<Test>> PostTest(Test test)
{
_context.Test.Add(test);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetTest), new { id = test.DataId }, test);
}
// DELETE: api/Test/5
[HttpDelete("{id}")]
public async Task<ActionResult<Test>> DeleteTest(int id)
{
var test = await _context.Test.FindAsync(id);
if (test == null)
{
return NotFound();
}
_context.Test.Remove(test);
await _context.SaveChangesAsync();
return test;
}
private bool TestExists(int id)
{
return _context.Test.Any(e => e.DataId == id);
}
}
}
名为 test.cs 的模型类:
using System;
using System.Collections.Generic;
namespace SimpleAPI.Models
{
public partial class Test
{
public int DataId { get; set; }
public string Data { get; set; }
}
}
我的 Startup.cs:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SimpleAPI.Models;
namespace SimpleAPI
{
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)
{
var connection = Configuration.GetConnectionString("myDatabase");
services.AddDbContext<appDBContext>(options => options.UseSqlServer(connection));
services.AddControllersWithViews();
}
// 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();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Test}/{action=Index}/{id?}");
//endpoints.MapControllers();
});
}
}
}
最后是我的 appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"myDatabase": "Server=server;Database=dbname;User ID=user;Password=***;"
}
}
解决方案
邮递员的问题比代码的问题更大。很棒的评论引导我了解 Postman 是如何发送数据的。尤其是 Max 的评论是正确的。
推荐阅读
- c# - 如何将字符串格式的不同日期转换为特定格式,如 YYYYMMDD
- css - 有条件地将覆盖的 SCSS 应用于 Angular 组件
- java - 在 Activity App Bar 上按下后转到带有它被调用的片段的父 Activity
- angular - p-menuitem 不是已知元素 - primeng
- ffmpeg - 未知编码器“libwebp”
- javascript - Chrome 插件无法在 Api 调用中使用自定义用户代理
- ssl - TLS 握手失败,但通信未关闭
- c++ - boost.geometry 多边形差异返回自相交结果(或错误地检测到它)
- ios - 带有偏移 UIImage 的 UIKit UIImageView
- javascript - 第二次点击Ajax刷新数据?