0

I have a problem with routing, since I created "BaseController". I use only 4 methods name GET, POST, PUT, DELETE, to make easiest calls from front-end. So, when I have this controller:

 [RoutePrefix("api/Router")]
public class RouterController : WifiBaseController
{
    UnitOfWork unitOfWork = new UnitOfWork();

    [JwtAuthentication]
    [HttpGet]
    [Route("")]
    public List<RouterDTO> Get()
    {
        List<router> routerx = unitOfWork.RouterRepository.Get(r => r.IsDeleted == false).ToList();
        List<RouterDTO> routerDTO = Mapper.Map<List<RouterDTO>>(routerx);
        foreach (var router in routerDTO.Where(x => x.Password != ""))
        {
            router.Password = null;
        }
        return routerDTO;
    }

    [HttpGet]
    [JwtAuthentication]
    [Route("{latitude}/{longitude}")]
    public List<RouterDTO> Get(double latitude, double longitude)
    {
        List<RouterDTO> routersDTO = new List<RouterDTO>();
        List<router> routers = new List<router>();

        var myLocation = GPSCalculation.CreatePoint(latitude, longitude);
        routers = unitOfWork.RouterRepository.Get(x => x.Location.Location.Distance(myLocation) < 2000 && x.IsDeleted == false).ToList();

        Mapper.Map(routers, routersDTO);
        foreach (var router in routersDTO.Where(x => x.Password != ""))
        {
            router.Password = "";
        }
        return routersDTO;
    }

And I made this call:

http://localhost:50919/api/Router?latitude=46.767&longitude=23.60

The methods that will be called it's first ...Why?

If I comment the first method, the API returns:

405 Method Not Allowed (The requested resource does not support http method 'GET')

0

1 Answer 1

3

Based on your route attribute in the second method:

[Route("{latitude}/{longitude}")]

The correct call with this route looks like it should be:

http://localhost:50919/api/Router/46.767/23.60
Sign up to request clarification or add additional context in comments.

4 Comments

After i've changed the attribute to [Route("{latitude}&{longitude}")], still not working. Returns same errors.. :(
@DavidPantea, Did you read The answer - its / (forward slash), not &. But this will result in Dots in URL causes 404 with ASP.NET mvc and IIS
@StephenMuecke, I want to keep this format to call "...Routers?latitude=46.767&longitude=23.60". It's possible?
@DavidPantea If you do want that url, then it will match your first public List<RouterDTO> Get() method (because the url does not contain route segments for latitude and longitude - only query string values). You would need to modify the first method to check if the query string values exist (or not) and then conditionally call the relevant section of code in an if/else block

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.