James Wierzba
James Wierzba

Reputation: 17508

.NET Core MVC Controller not routing to POST function

I've created a new controller in my .NET Core MVC rest service and it's not routing to the POST function when I call the API.

I've got existing controllers that are in the exact same format as this one, I can't determine why the service function is not getting called. The request is always returning a generic 500 error.

URL: https://address:port/api/TOSResponse

using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using MyProject.ActionFilters;
using MyProject.Common.Exceptions;
using MyProject.Services;

namespace MyProject.Controllers
{

    [ServiceFilter(typeof(KeyValidatorFilter))]
    [Route("api/[controller]")]
    public class TOSResponseController : Controller
    {

        private ITOSResponseService _tosResponseService;

        public TOSResponseController(ITOSResponseService tosResponseService)
        {
            _tosResponseService = tosResponseService;
        }

        [HttpPost]
        public IActionResult Post([FromBody] List<Models.TermsOfServiceResponse> tosResponses)
        {
            try
            {
                _tosResponseService.InsertOrUpdate(tosResponses);
                return new OkResult();
            }
            catch (PLException ex)
            {
                return new BadRequestObjectResult(ex.ErrorValue);
            }
        }

    }
}

Upvotes: 1

Views: 976

Answers (2)

James Wierzba
James Wierzba

Reputation: 17508

I discovered the issue. The Controller was not even being instantiated because a reference to ITOSResponseService could not be resolved, I neglected to register the service with the IoC container in the ConfigureServices function in Startup.cs:

public class Startup
{
    //...
    public void ConfigureServices(IServiceCollection services)
    {
        //...
        services.AddTransient<ITOSResponseService, TOSResponseService>();
        //...  
    }
    //...
}

Upvotes: 1

noBreakingChanges
noBreakingChanges

Reputation: 603

The problem may exist in the KeyValidatorFilter that you have added as a ServiceFilter. This would be getting hit first and could be throwing a 500 back to the user before it hits your controller. Are you using this filter in your other controllers? Try adding a breakpoint in the filter in debug mode and see what happens.

Upvotes: 1

Related Questions