ASP.NET具有相同方法名称和不同参数时的路由问题

时间:2018-05-24 17:53:04

标签: c# asp.net-mvc asp.net-web-api asp.net-web-api-routing

我遇到路由问题,因为我创建了" BaseController"。 我只使用4个方法命名GET,POST,PUT,DELETE,以便从前端进行最简单的调用。 所以,当我有这个控制器时:

 [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;
    }

我接到了这个电话:

  

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

首先要称之为的方法......为什么?

如果我评论第一种方法,API会返回:

  

405方法不允许(请求的资源不支持http方法&#39; GET&#39;)

1 个答案:

答案 0 :(得分:3)

根据第二种方法中的路线属性:

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

使用此路线的正确呼叫看起来应该是:

http://localhost:50919/api/Router/46.767/23.60