简体   繁体   中英

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

I am trying to make an api using asp.net core web api. Although I put [HttpGet] and [HttpPost] tags above the methods, only the function that takes parameters appears in the interface.

The methods I created do not appear in the swagger interface

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());
        }
    }
}

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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