web-dev-qa-db-ja.com

Asp.net Mvc 4およびWeb Apiのルーティング

次の2つのルートルールを一緒に使用できますか?

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

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

コントローラーごとに= _FruitApiController:ApiController_と言うと、次のものが欲しい

  1. List<Fruit> Get() = _api/FruitApi/_

  2. List<Fruit> GetSeasonalFruits() = _api/FruitApi/GetSeasonalFruit_

  3. Fruit GetFruits(string id) = _api/FruitApi/15_

  4. Fruit GetFruitsByName(string name) = _api/FruitApi/GetFruitsByName/Apple_

これで私を助けてください。ありがとう

37
Yasser

いくつかのルートがあります。

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "ApiById",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional },
            constraints: new { id = @"^[0-9]+$" }
        );

        config.Routes.MapHttpRoute(
            name: "ApiByName",
            routeTemplate: "api/{controller}/{action}/{name}",
            defaults: null,
            constraints: new { name = @"^[a-z]+$" }
        );

        config.Routes.MapHttpRoute(
            name: "ApiByAction",
            routeTemplate: "api/{controller}/{action}",
            defaults: new { action = "Get" }
        );
    }
}
57
Darin Dimitrov
config.Routes.MapHttpRoute(
            name: "ApiByName",
            routeTemplate: "api/{controller}/{action}/{name}",
            defaults: null,
            constraints: new { name = @"^[a-z]+$" }
        );