相关疑难解决方法(0)

在ASP.NET Web API中使用多个Get方法进行路由

我正在使用Web Api和ASP.NET MVC,我对它很新.我已经在asp.net网站上进行了一些演示,我正在尝试执行以下操作.

我有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
}
Run Code Online (Sandbox Code Playgroud)

对于上面的所有方法,我希望我的web api有点像下面所示

  • 列表Get()= api/customers/
  • Customer GetCustomerById(string Id)= api/customers/13
  • 列出GetCustomerByCurrentMonth()= /customers/currentMonth
  • Customer GetCustomerByUsername(string username)= /customers/customerByUsername/yasser

我尝试对路由进行更改,但由于我是新手,因此无法理解.

所以,请一些人帮助我理解并指导我如何做到这一点.谢谢

c# asp.net-web-api asp.net-web-api-routing

60
推荐指数
5
解决办法
13万
查看次数

如何正确映射WebAPI路由

我正在使用Web API为类似Twitter的网站构建API,并且无法映射路由

我对User控制器执行以下操作:

public User Get(string firstname, string lastname)
public User Get(Guid id)
public User Friends(Guid id)
public User Followers(Guid id)
public User Favorites(Guid id)
Run Code Online (Sandbox Code Playgroud)

所需的路线和生成的文档应该是:

api/users?firstname={firstname}&lastname={lastname}
api/users/{id}
api/users/{id}/friends
api/users/{id}/followers
api/users/{id}/favorites
Run Code Online (Sandbox Code Playgroud)

在WebApiConfig.cs中我有:

config.Routes.MapHttpRoute(
    "2",
    "api/{controller}/{id}",
    new { action = "get", id = RouteParameter.Optional }
);


config.Routes.MapHttpRoute(
     "1",
     "api/{controller}/{id}/{action}"
);
Run Code Online (Sandbox Code Playgroud)

如何正确映射WebAPI路由?

c# asp.net asp.net-mvc asp.net-web-api asp.net-web-api-routing

17
推荐指数
3
解决办法
5万
查看次数