无法实现自定义MapHTTPRoute

时间:2019-06-20 03:23:05

标签: c# api

我正在使用Microsoft.Azure.Mobile.Server来开发MobileAPI,其中我已经通过多个Get方法实现了控制器类,但是当我尝试从Post Man调用时,只有默认的get方法有效。

尝试在startup.MobileApp中进行以下设置

config.Routes.MapHttpRoute(
            name: "ApiWithActionName",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional });

   [MobileAppController]
    public class CustomController : ApiController
    {
        [HttpGet]
        //[ActionName("GetDifficultyOrderBY")]
        [Route("Custom/GetDifficultyOrderBY")]
        public List<TrainingDifficulty> GetDifficultyOrderBY()
        {
            List<TrainingDifficulty> trainingDifficultyList = null;
            using (var _dbcontext = new NarbisMobileAppContext()){
                trainingDifficultyList = _dbcontext.TrainingDifficulties.OrderBy(x => x.Name).ToList();
            }
            return trainingDifficultyList;
        }
        [HttpGet]
        //[ActionName("GetTrainingTimeOrderBY")]
        [Route("Custom/GetTrainingTimeOrderBY")]
        public List<TrainingTime> GetTrainingTimeOrderBY()
        {
            List<TrainingTime> trainingTimeList = null;
            using (var _dbcontext = new NarbisMobileAppContext())
            {
                trainingTimeList = _dbcontext.TrainingTimes.OrderBy(x => Convert.ToInt32(x.Name)).ToList();
            }
            return trainingTimeList;
        }

    }

1 个答案:

答案 0 :(得分:0)

在CustomController级别中,添加属性[RoutePrefix(“ api / Custom”)]并从Route中删除“ Custom”,即[Route(“ Custom / GetTrainingTimeOrderBY”)]将此属性更改为[Route(“ GetTrainingTimeOrderBY”)]]

现在像 api / Custom / GetTrainingTimeOrderBY

一样访问它

希望这项工作。