Home > Software engineering >  Multiple Http Gets in one controller
Multiple Http Gets in one controller

Time:12-16

My Web API application is working and I can GET with the primary key, but I need to be able to GET with other fields, like the Widgetname, and I know I need to specify '[Route("api/[controller]/[action]")]' for this to work. The action 'GetByID' works, but not the 'GetByName' action. I change the names of the actual work I am doing to 'Widget', so I may have not got everything renamed correctly. The code compiles, but when I attempt the API call to 'GetByName' I get a 404 error. Here is my code:

namespace WidgetAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WidgetStuffController : ControllerBase
    {
        private readonly WidgetDbContext _context;

        public WidgetStuffController(WidgetDbContext context)
        {
            _context = context;
        }

        // GET: api/WidgetStuff
        [HttpGet]
        public async Task<ActionResult<IEnumerable<WidgetStuff>>> GetWidgetStuff()
        {
            return await _context.StuffHosts.ToListAsync();
        }

        // GET: api/WidgetStuff/GetByID
        [HttpGet("{ID}"), ActionName("GetByID")]
        public async Task<ActionResult<WidgetStuff>> GetByUUID(string ID)
        {
            var widgetStuff = await _context.StuffHosts.FindAsync(ID);

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

        // GET: api/WidgetStuff/GetByName
        [HttpGet("{Name}"), ActionName("GetByName")]
        public async Task<ActionResult<WidgetStuff>> GetByName(string Name)
        {
            var widgetStuff = await _context.StuffHosts.FindAsync(Name);

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

    }
}

If you need to see my DBContext or Models, let me know.

CodePudding user response:

Your code is probably hitting the controller and then actively returning the 404.

    [HttpGet("{Name}"), ActionName("GetByName")]
    public async Task<ActionResult<WidgetStuff>> GetByName(string Name)
    {
        var widgetStuff = await _context.StuffHosts.FindAsync(Name);

        if (widgetStuff == null)
        {
            return NotFound(); <---- THIS RETURNS A 404
        }
        return widgetStuff;
    }

The problem is FindAsync which works on the primary key (which is presumably Id in your table).

Try replacing this line:

var widgetStuff = await _context.StuffHosts.FindAsync(Name);

with:

var widgetStuff = await _context.StuffHosts.SingleOrDefaultAsync(a => a.Name == Name);

CodePudding user response:

I don't think your code is hitting the end point

this is how I would make the api call in dot.net core.

[HttpGet]
[Route("GetByName/{Name}")]
public async Task<ActionResult<WidgetStuff>> GetByName(string Name)
        {
            var widgetStuff = await _context.StuffHosts.FindAsync(Name);

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

the end point

localhost:5000/api/yourcontroller/GetByName/nospaces_unless_escaped

set a break point and see if you code is throwing a error.

[ProducesResponseType(typeof(ReviewView), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ReviewView), StatusCodes.Status500OK)] [ProducesResponseType(typeof(ReviewView), StatusCodes.Status404OK)]

  • Related