0
votes

I'm working with the ASP.NET core example project. To create the project run

dotnet new lambda.DynamoDBBlogAPI --profile default --region us-east-1

This creates two controllers by default.

  • Controllers\S3ProxyController - Web API controller for proxying an S3 bucket
  • Controllers\ValuesController - example Web API controller

I'm trying to add an additional controller .. but I'm clearly missing something as the controller does not seem to register.

If I simply add a class that looks like this to the Controllers folder.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Mvc;

namespace FriendHelper.Controllers
{
    [Route("api/animals")]
    class AnimalController : Controller
    {
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "Lion", "Tiger" };
        }

    }

}

This doesn't seem to register, so going to http://localhost:5000/api/animals simply returns a 404

I don't have much experience with ASP.NET MVC so I'm sure there's something silly I'm missing but I can't see where these other controllers are registered.

1
If you're asking does the service crash .. no, you just get a 404. - Warrick FitzGerald
In regular MVC you should inherit from ApiController, not controller. (I guess it is the same in core) - Ziv Weissman
Thanks @ZivWeissman but that doesn't seem to be the case in core. I also copied one of the other controllers that is working, but it's almost like my new controller is registered somehow. - Warrick FitzGerald
The other controllers also use the "route" attribute? - Ziv Weissman
Yes, the other two have the following attribute out of the box [Route("api/[controller]")] ... but even hard coding it to [Route("api/aaa")] and then hard coding my controller to [Route("api/bbb")] leaves the origional working, but my controller won't work. - Warrick FitzGerald

1 Answers

2
votes

You should make the controller class public

public class AnimalController : Controller{
...
}

Values controller won't work if you delete the public keyword either. At least doesn't work for me :)