blazor服务器应用程序中发布创建记录webapi的问题

问题描述

我创建了一个blazor服务器应用程序项目,并且正在使用内置的webapi框架

我正在检查邮递员中的创建记录webapi,但它提供的204内容表示我的webapi运行,但返回204内容,请参见下图

https://i.stack.imgur.com/bdfFc.png

//邮递员中的webapi测试,但返回204内容

postman->header(Content-Type-application/json)

blazor服务器应用

EmpsController.cs

namespace CrudBlazorServerApp.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmpsController : ControllerBase
    {       
        private readonly sqldbcontext _context;

        public EmpsController(sqldbcontext context)
        {
            _context = context;
        }

        // GET: api/Emps
        [HttpGet]
        public async Task<ActionResult<IEnumerable<Emp>>> Getemps()
        {
            return await _context.emps.ToListAsync();
        }
        
        // GET: api/Emps/5
        [HttpGet("{id}")]
        public async Task<ActionResult<Emp>> GetEmp(int id)
        {
            var emp = await _context.emps.FindAsync(id);

            if (emp == null)
            {
                return NotFound();
            }

            return emp;
        }

        [HttpPost]
        public async Task<ActionResult<Emp>> Postemp(Emp emp)  //here I am facing issue record is not created
        {
            _context.emps.Add(emp);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetEmp",new { id = emp.empid },emp);
        }

Startup.cs

namespace CrudBlazorServerApp
{
    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.
        // For more @R_529_4045@ion on how to configure your application,visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();
            
            services.AddControllers()
                .AddJsonoptions(options => options.JsonSerializerOptions.IgnoreNullValues = true);
            services.AddDbContext<sqldbcontext>(options => options.UsesqlServer(Configuration.GetConnectionString("sqlserverconnn")));
            services.AddRazorPages();
            services.AddServerSideBlazor();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
        {
            using (var serviceScope = app.applicationservices.GetService<IServiceScopeFactory>().CreateScope())
            {
                var context = serviceScope.ServiceProvider.GetrequiredService<sqldbcontext>();
                context.Database.EnsureCreated();
            }

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios,see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseCors(policy =>
    policy.WithOrigins("https://localhost:44399")  // client address 
    .AllowAnyMethod()
    .WithHeaders(HeaderNames.ContentType));
            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapBlazorHub();
                endpoints.MapFallbackToPage("/_Host");
            });
        }
    }
}

Database Diagram

为什么没有提供204内容

.net核心项目版本: netcoreapp3.1

我正在尝试什么:

我对此行发表评论

await _context.SaveChangesAsync(); 

但未创建记录

请帮助

哪个地方需要更正?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)