1

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

    }
}
2
  • You need to show the URL you're using and the ServiceFilter logic. Commented Apr 6, 2017 at 18:54
  • oops one moment i will edit the answer Commented Apr 6, 2017 at 19:01

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This was my thought, but it is also not being hit
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.