0

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.

5
  • If you're asking does the service crash .. no, you just get a 404. Commented Nov 18, 2017 at 20:30
  • In regular MVC you should inherit from ApiController, not controller. (I guess it is the same in core) Commented Nov 18, 2017 at 20:42
  • 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. Commented Nov 18, 2017 at 21:09
  • The other controllers also use the "route" attribute? Commented Nov 18, 2017 at 21:12
  • 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. Commented Nov 18, 2017 at 21:57

1 Answer 1

2

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 :)

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

1 Comment

LOL .. You know sometimes I just want to smack myself. I knew it was something so obvious. Thanks!

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.