64

私は ASP.NET MVC で Web Api を使用していますが、これは初めてです。私はasp.net Webサイトでいくつかのデモを行っており、次のことをしようとしています。

次のシグネチャを持つ 4 つの get メソッドがあります。

public List<Customer> Get()
{
    // gets all customer
}

public List<Customer> GetCustomerByCurrentMonth()
{
    // gets some customer on some logic
}

public Customer GetCustomerById(string id)
{
    // gets a single customer using id
}

public Customer GetCustomerByUsername(string username)
{
    // gets a single customer using username
}

上記のすべてのメソッドについて、Web API を以下のようにしたいと考えています。

  • リスト Get() =api/customers/
  • 顧客 GetCustomerById(文字列 ID) =api/customers/13
  • List GetCustomerByCurrentMonth() =/customers/currentMonth
  • 顧客 GetCustomerByUsername(文字列ユーザー名) =/customers/customerByUsername/yasser

ルーティングを変更してみましたが、初心者なのであまり理解できませんでした。

ですから、これをどのように行うべきかを理解し、案内してくれる人がいます。ありがとう

4

10 に答える 10

75

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

Darin Dimitrovは、私のために働いている非常に良い答えを投稿しました。

それは言う...

いくつかのルートを持つことができます:

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" }
        );
    }
}
于 2012-10-08T08:48:58.780 に答える
49

最初に、アクションを先頭に新しいルートを追加します。

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

次に、ActionName属性を使用してマップします。

[HttpGet]
public List<Customer> Get()
{
    //gets all customer
}

[ActionName("CurrentMonth")]
public List<Customer> GetCustomerByCurrentMonth()
{
    //gets some customer on some logic
}

[ActionName("customerById")]
public Customer GetCustomerById(string id)
{
    //gets a single customer using id
}

[ActionName("customerByUsername")]
public Customer GetCustomerByUsername(string username)
{
    //gets a single customer using username
}
于 2012-10-08T05:43:53.003 に答える
20

また、セット ルートのルート オン アクションを指定します。

[HttpGet]
[Route("api/customers/")]
public List<Customer> Get()
{
   //gets all customer logic
}

[HttpGet]
[Route("api/customers/currentMonth")]
public List<Customer> GetCustomerByCurrentMonth()
{
     //gets some customer 
}

[HttpGet]
[Route("api/customers/{id}")]
public Customer GetCustomerById(string id)
{
  //gets a single customer by specified id
}
[HttpGet]
[Route("api/customers/customerByUsername/{username}")]
public Customer GetCustomerByUsername(string username)
{
    //gets customer by its username
}
于 2016-11-03T08:56:06.597 に答える
5

これには1つのルートだけで十分です

config.Routes.MapHttpRoute("DefaultApiWithAction", "{controller}/{action}");

また、すべてのアクションで属性HttpGetまたはHttpPostを指定する必要があります。

[HttpGet]
public IEnumerable<object> TestGet1()
{
    return new string[] { "value1", "value2" };
}

[HttpGet]
public IEnumerable<object> TestGet2()
{
    return new string[] { "value3", "value4" };
}
于 2013-11-06T11:57:08.620 に答える
0

ルーティングを変更する必要がない場合もあります。customersController.cs ファイルに次の 4 つのメソッドを追加するだけです。

public ActionResult Index()
{
}

public ActionResult currentMonth()
{
}

public ActionResult customerById(int id)
{
}


public ActionResult customerByUsername(string userName)
{
}

関連するコードをメソッドに入れます。デフォルトのルーティングが提供されると、指定された URL のアクションとパラメーターに基づいて、コントローラーから適切なアクションの結果が得られるはずです。

デフォルト ルートを次のように変更します。

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Api", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
于 2012-10-08T05:08:54.393 に答える