David Pantea
David Pantea

Reputation: 101

ASP.NET Routing problems when ii have same methods name with different params

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

Upvotes: 0

Views: 172

Answers (1)

Micah
Micah

Reputation: 471

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

Upvotes: 3

Related Questions