2
votes

I'm trying to build an API within the ASP.NET Core framework. I've made a basic controller which just returns a string:

namespace Dojo_Api.Controllers.Forum
{
    //[Authorize]
    [Route("api/[controller")]
    public class ForumController : Controller
    {
        private MainContext _context;

        public ForumController(MainContext context)
        {
            _context = context;
        }

        [HttpGet]
        public string Get()
        {
            return "string";
        }
    }
}

Now when I try to access the API via Postman, I receive a 500 Internal Server Error:

enter image description here

I have already tried changing the port, which didn't work. Does anyone know a solution for this problem?

Thanks in advance!

2
Did you debug and check if the controller is hit or not? If it's being executed then is there any exceptions during execution?Pratik Gaikwad
@Peurr your ForumController constructor contains a parameter. have you injected the dependency ?gypsyCoder
Add a parameterless constructor: public ForumController(){}Marcus Höglund
Controller route missing a closing square bracket here [Route("api/[controller")] unless that was a typo when question was writtenNkosi
Just curious more than anything, where have you seen [Route("api/[controller]")] used?Mark C.

2 Answers

3
votes

Controller route missing a closing square bracket here [Route("api/[controller")] unless that was a typo when question was written.

it should be

[Route("api/[controller]")]
public class ForumController : Controller { ... }
1
votes
[RoutePrefix("api/forum")]
public class ForumController : Controller
{
    private MainContext _context;

    public ForumController()
    {
        _context = new MainContext();
    }

    [HttpGet]
    public string Get()
    {
        return "string";
    }
}

Please try this