简体   繁体   English

Asp.net web api 发布和获取方法不可见

[英]Asp.net web api post and get methods are not visible

I am trying to make an api using asp.net core web api.我正在尝试使用 api 使用 asp.net 核心 web Z8A5DA52ED126447135AZE70A8A5 Although I put [HttpGet] and [HttpPost] tags above the methods, only the function that takes parameters appears in the interface.虽然我在方法上面放了[HttpGet]和[HttpPost]标签,但是界面中只出现了带参数的function。

The methods I created do not appear in the swagger interface我创建的方法没有出现在swagger界面中

namespace APIproje.Controllers {

    public class UsersController : ControllerBase {

        private readonly IKullnicilar _userRepositories;
        public UsersController(IKullnicilar userRepositories) {
            _userRepositories = userRepositories;
        }
        [HttpGet]
        public async Task<IEnumerable<Users>> GetUsers() {
            return await _userRepositories.Get();
        }
        [HttpGet("{id}")]
        public async Task<ActionResult<Users>> GetUsers(int id) {
            return await _userRepositories.Get(id);
        }
        [HttpPost]
        public async Task<ActionResult<Users>> PostUsers([FromBody] Users user)
        {
            var newUser = await _userRepositories.Create(user);
            return CreatedAtAction(nameof(GetUsers), new { id = newUser.id }, newUser);
        }
    }
}

Running:跑步:

在此处输入图像描述

Where am I wrong?我哪里错了?

you can use您可以使用

[ApiController]
[Route("you'r route")]

like this:像这样:

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
    [ApiController]
    [Route("api/")]
    public class UsersController : ControllerBase
    {

        [HttpGet]
        public async Task<IEnumerable<object>> GetUsers()
        {
            return new List<object>();
        }
        [HttpGet("{id}")]
        public async Task<ActionResult<object>> GetUsers(int id)
        {
            return Ok(new object());
        }
        [HttpPost]
        public async Task<ActionResult<object>> PostUsers([FromBody] object user)
        {
            return Ok(new object());
        }
    }
}

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM