我正在ASP.NETMVC Core上创建简单的CRUD操作以及React.我可以成功地添加记录和查看记录.但我无法删除和更新记录.我通过postman 判断,工作正常,我可以删除和更新记录,当我通过react ,更新和删除功能不起作用.我目前所try 的,我附在下面.

Error show us on the console

Failed to load resource: the server responded with a status of 404 () this error is throwing on the console

StudentController.cs

     using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using webb.Model;

namespace webb.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class StudentController : ControllerBase
    {
        private readonly StudentDbContext _studentDbContext;


     
        [HttpPatch]
        [Route("UpdateStudent")]
        public async Task<Student> UpdateEmployee(Student objStudent)
        {
            _studentDbContext.Entry(objStudent).State = EntityState.Modified;
            await _studentDbContext.SaveChangesAsync();
            return objStudent;
        }

        [HttpDelete]
        [Route("DeleteStudent")]
        public bool DeleteEmployee(int id)
        {
            bool result = false;
            var student = _studentDbContext.Student.Find(id);
            if (student != null)
            {
                _studentDbContext.Entry(student).State = EntityState.Deleted;
                _studentDbContext.SaveChanges();
                result = true;
            }
            else
            {
                result = false;
            }
            return result;
        }
    }
}

React Code

  async function DeleteEmployee(id) {
 

  await axios.delete("https://localhost:7205/api/Student/DeleteStudent" + id);
   alert("Employee deleted Successfully");
   Load();
  }
 
  async function update(event) {
    event.preventDefault();
    try {

  await axios.patch("https://localhost:7205/api/Student/UpdateStudent"+ employees.find((u) => u.id === id).id || id,
        {
        id: id,
        stname: stname,
        course: course,
      
        }
      );
      alert("Registation Updateddddd");
     
      Load();
    } catch (err) {
      alert(err);
    }
  }
 
  return (
    

Check Through Swagger Update enter image description here

Delete enter image description here

Program.cs

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using webb.Model;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();


builder.Services.AddDbContext<StudentDbContext>(options =>
   options.UseSqlServer(builder.Configuration.GetConnectionString("StudentDbContext")));

var app = builder.Build();

app.UseCors(policy => policy.AllowAnyHeader()
                            .AllowAnyMethod()
                            .SetIsOriginAllowed(origin => true)
                            .AllowCredentials());


// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

推荐答案

就像 comments 中说的那样:

如果使用id作为查询字符串调用Delete接口,则接口中应设置FromUri.

或者,您可以将路由更改为[Route("DeleteStudent/{id}")]和前面await axios.delete("https://localhost:7205/api/Student/DeleteStudent/" + id);

Reactjs相关问答推荐

使用useEffect挂钩获取数据

无法在jest中发布行动

条件索引路由

404使用GitHub操作部署Next.js应用程序时出错

使用筛选器的Primereact DataTable服务器端分页?

禁止直接访问Next.js中的页面,同时允许从应用程序内部访问

React useEffect 问题...异步函数内的变量正在获取延迟的更新值

MUI 日期 Select 器 - 最小日期混乱

使用 React + Vite 范围化 CSS

Stripe Checkout:是否可以创建不自动续订的订阅?

在 Nextjs13 应用程序目录中呈现从 supabase 获取的数据

React Three Fiber mesh 标签在此浏览器中无法识别

react 本机日历

getAttribute 函数并不总是检索属性值

使用 yarn 编译 protobuf js 时出现错误pbjs: command not found

未捕获的错误:操作必须是使用 redux/toolkit 的普通对象

NextJs - 如何从站点 map 生成器中删除重复的 URL?

交换键仍然引入了 fresh 的 DOM 元素

将 set statehook 函数传递给子路由组件.解构错误

RTK 查询 POST 方法不会改变数据