0

I having a issue with attribute based routing not working. I have set my prefix (http://localhost:6600/api/camps/ATL2018/talks) as you can see below and set a default route and it seems to work fine.

namespace TheCodeCamp.Controllers
{
    [RoutePrefix("api/camps/{moniker}/talks")]
    public class TalkController : ApiController

    {
        private readonly ICampRepository _repositiry;
        private readonly IMapper _mapper;

        public TalkController(ICampRepository repository, IMapper mapper)
        {
            _repositiry = repository;
            _mapper = mapper;

        }

        [Route()]
        public async Task<IHttpActionResult> Get(string moniker, bool includeSpeakers = false)
        {
            try 
            {
                var results = await _repositiry.GetTalksByMonikerAsync(moniker, includeSpeakers);

                return Ok(_mapper.Map<IEnumerable<TalkModel>>(results));
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }

        }

        [Route("{id:int}")]
        public async Task<IHttpActionResult> Get(string moniker,int id, bool includeSpeakers)
        {

            try 
            {
                var result = await _repositiry.GetTalkByMonikerAsync(moniker, id, includeSpeakers);
                if (result == null) return NotFound();

                return Ok(_mapper.Map<TalkModel>(result));
            }

            catch(Exception ex)
            {
                return InternalServerError(ex);

            }

        }

    }
}

This issue is when I run the below URL it returns the Not Found

 http://localhost:6600/api/camps/ATL2018/talks/1

There is a talks for Id 1 and issue seems to be that it is not even hitting the [Route("{id:int}")] as I set a break point against it and it returns Not Found without hitting the break point.

Any advise would be greatly appreciated.

1
  • The parameter list is not matching the the input. In c# a method like Get can have multiple methods with different number of parameters. So it is not getting into the method because the parameter are not the same as you defined method. Commented Nov 8, 2020 at 14:43

1 Answer 1

1

In your second Get, your argument includeSpeakers is not provided in your Route and is not set to optional. I am not sure if your problem comes from there, but I think that the way you define your second endpoint can lead to a routing problem since you are not providing all the input args.

[Route("{id:int}")]
public async Task<IHttpActionResult> Get(string moniker,int id, bool includeSpeakers = false)
Sign up to request clarification or add additional context in comments.

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.