如何在MVC中使用/配置路由

时间:2016-10-11 15:04:26

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

当它被称为/ api / Games / aaa时,我想要运行方法Get(string deviceId = "")。但当它被称为api / Games / GameById / TaskId时,我想要运行方法GameById(int id)。但它不起作用,特别是第二种方法。

控制器:

public class GamesController : ApiController
    {
        private List<Game> _gamesRepository;


        public GamesController()
        {
            _gamesRepository = CreateGamesRepository();
        }



        // GET api/Games/DeviceId
        [HttpGet]
        public IEnumerable<Game> Get(string deviceId = "")
        {
            IEnumerable<Game> result;

            if (String.IsNullOrEmpty(deviceId))
            {
                result = _gamesRepository.AsEnumerable();
            }
            else
            {
                result = _gamesRepository.Where(x => x.DeviceId == deviceId).AsEnumerable();
            }
            return result;

        }


        // GET api/Games/GameById/Id
        [HttpGet]
        public IEnumerable<Game> GameById(int id)
        {
            IEnumerable<Game> result;

            result = _gamesRepository.Where(x => x.TaskId == id).AsEnumerable();

            return result;

        }

路由:

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
            name: "GamesRoute",
            url: "api/{controller}/{deviceId}",
            defaults: new { controller = "controller", action = "get", deviceId = UrlParameter.Optional }
        );

            routes.MapRoute(
           name: "GamesRoute2",
           url: "api/games/GameById/{id}",
                // defaults: new { id = UrlParameter.Optional }
                defaults: new { controller = "games", action = "GameById" },
                constraints: new { id = @"\d+" }
       );

型号:

   public class Game
    {
        public int TaskId { get; set; }
        public string SalesForceId { get; set; }
        public string Name { get; set; }
        public string Thumbnail { get; set; }
        public string DeviceId { get; set; }
    }
}

3 个答案:

答案 0 :(得分:0)

您可以尝试将RouteParameters设置为可选吗?

defaults: new { id = RouteParameter.Optional }

答案 1 :(得分:0)

地图路线的顺序很重要,因为第一条路线匹配胜利,框架停止寻找更多路线。

应在更具体的路线之后添加一般路线。

public static class WebApiConfig {

    public static void Register(HttpConfiguration config) {

         config.Routes.MapRoute(
           name: "GamesRoute2",
           routeTemplate: "api/games/GameById/{id}",
           defaults: new { controller = "games", action = "GameById" },
           constraints: new { id = @"\d+" }
        );

        config.Routes.MapRoute(
            name: "GamesRoute",
            routeTemplate: "api/{controller}/{deviceId}",
            defaults: new { controller = "controller", action = "get", deviceId = UrlParameter.Optional }

        );
     }
}

答案 2 :(得分:0)

我个人认为您不应该在Web API配置中映射每一条路线。使用可持续的架构并支持您希望使用的设计类型。

在您的情况下,我将在WebApiConfig中使用此配置为我的API使用更具描述性的URI:

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

这样做可以让您在URI中指定/action,让您在通话中非常明确。您的终端将成为:

/api/games/getById?id=1
/api/games/get?"Test"

修改

如果您希望路线完全匹配api/games/{parameter},那么我会在该方法上添加路线属性,如下所示:

[Route("api/games/{id}")]
public IEnumerable<Game> Get(string deviceId = "")

希望这有帮助。