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

Nkosi
  • 235,767
  • 35
  • 427
  • 472
David Pantea
  • 101
  • 2
  • 9

1 Answers1

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
Micah
  • 421
  • 1
  • 5
  • 11
  • After i've changed the attribute to [Route("{latitude}&{longitude}")], still not working. Returns same errors.. :( – David Pantea May 24 '18 at 22:16
  • @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](https://stackoverflow.com/questions/11728846/dots-in-url-causes-404-with-asp-net-mvc-and-iis) –  May 24 '18 at 22:46
  • @StephenMuecke, I want to keep this format to call "...Routers?latitude=46.767&longitude=23.60". It's possible? – David Pantea May 24 '18 at 23:13
  • 1
    @DavidPantea If you do want that url, then it will match your first `public List 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 –  May 24 '18 at 23:21