【问题标题】:not enter HTTPPOST Action in asp core web api不在 asp core web api 中输入 HTTPPOST Action
【发布时间】:2019-04-21 01:12:55
【问题描述】:

我在asp core2.2 中创建了一个Web 服务并从客户端(angular6) 发送数据。

我的控制器在Admin 区域。

这个启动:

 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseSignalR(routes =>
        {
            routes.MapHub<CrudRealTime>("/CrudRealTime");
        });
        app.UseCors("CorsPolicy");
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "areas",
                template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
            );
        });
    }

我使用此地址访问 RoleManager 控制器中的Create RolesActoin:

https://localhost:44390/api/role/createrole

但它没有进入行动。当我使用这个角色时,它可以工作:https://localhost:44390/api/role/GetRoles 但我不知道是什么问题以及我该如何解决这个问题。

上次我运行该项目并从客户端提供数据但现在它不起作用。我没有更改服务器代码中的任何内容。

我该如何解决这个问题????

角色动作

 [HttpPost("CreateRole")]
    public async Task<IActionResult> CreateRole([FromBody]RolePostModel model)
    {
        if (ModelState.IsValid)
        {

            var result = await _roleManag.CreateAsync(new Role(model.description, model.rolelevel, model.name));
            if (result.Succeeded)
            {
                return Ok(Messagesresx.Success_Add_Role);
            }
            else
            {
                return Content(Messagesresx.Fail_Add_Role_In_DataBase);
            }
        }
        else
        {
            return BadRequest();
        }
    }

【问题讨论】:

  • 完成您的示例并提及整个控制器类及其属性。
  • 尝试 POST 到 https://localhost:44390/api/role/createrole 时遇到什么错误?

标签: asp.net asp.net-web-api asp.net-core-2.0 asp.net-core-webapi asp.net-core-2.1


【解决方案1】:

你能通过 Postman 打 CreateRole 吗?如果您在 Json 中发布,请尝试将接受标头设置为 application/Json。 如果它成功命中,则意味着您需要在 WebAPI 启动中配置 InputFormatter 以接受您从 Angular 应用程序发布的格式,或者尝试更改您发送的格式。

【讨论】:

    猜你喜欢
    • 2018-12-30
    • 2021-08-27
    • 1970-01-01
    • 2019-05-15
    • 2023-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    相关资源
    最近更新 更多