【问题标题】:c# MapHttpAttibuteRoutes cannot use same action with different parameter?c# MapHttpAttibuteRoutes 不能使用不同参数的相同操作?
【发布时间】:2018-09-07 01:41:21
【问题描述】:

我是 c# 的新手,我被属性路由卡住了。

现在我想为 GET、POST、PUT 和 DELETE 用户创建一个 API。

我不知道如何描述这个问题,我发现最熟悉的不是我的案例的正确答案。

How to use Route attribute to bind query string with web API?

[HttpGet]
public class UsersController : ApiController
{
    // GET /api/users
    [HttpGet]
    [Route("")]
    public IEnumerable<User> GetUsers()
    {
        return _context.Users.ToList();
    }

    [Route("api/users/{FirstName?}/{LastName?}")]
    public IEnumerable<User> GetByFirstName(string FirstName = null, string LastName = null)
    { 
        var users = Enumerable.Empty<User>();
        if (FirstName != null && LastName != null)
            users = _context.Users.Where(u => (u.FirstName == FirstName) && (u.LastName == LastName));
        else if (FirstName != null)
            users = _context.Users.Where(u => u.FirstName == FirstName);
        else if (LastName != null)
            users = _context.Users.Where(u => u.LastName == LastName);

        return users;
    }
}

这是我的控制器所拥有的,下面是我所拥有的 WebApiConfig.cs 基本上我用过 config.MapHttpAttributeRoutes();

    public static void Register(HttpConfiguration config)
    {
      // Web API configuration and services
        config.MapHttpAttributeRoutes();

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

为什么我不能调用 api http://localhost:50861/api/users?LastName=someName&FirstName=someName

任何建议都非常感谢。或者这个问题的任何链接也有帮助!谢谢

编辑:我刚刚更新了我的代码的另一部分。因为当我使用上面的api查询时,我得到了这个/api/users的结果。

【问题讨论】:

    标签: c# rest api


    【解决方案1】:

    如下修改你的GetByFirstName

    [HttpGet]
    [Route("api/users/name")]
    public IEnumerable<User> GetByFirstName(string FirstName = null, string LastName = null)
    { 
        var users = Enumerable.Empty<User>();
        if (FirstName != null && LastName != null)
            users = _context.Users.Where(u => (u.FirstName == FirstName) && (u.LastName == LastName));
        else if (FirstName != null)
            users = _context.Users.Where(u => u.FirstName == FirstName);
        else if (LastName != null)
            users = _context.Users.Where(u => u.LastName == LastName);
    
        return users;
    }
    

    然后你的网址会像

    GET http://localhost:50822/api/users/name?FirstName=Will&LastName=abc
    

    【讨论】:

    • @William Shu,查看答案可能对你有帮助:)
    • 很高兴听到你解决了你的问题 :)
    【解决方案2】:

    您正在定义路线

    [Route("api/users/{FirstName?}/{LastName?}")]
    

    所以你的电话应该像

    http://localhost:50861/api/users/someName/someName
    

    如果你想通过查询字符串调用 api,那么你的路由应该定义为

    [Route("api/users")]
    

    但它会与您的 GetUsers 方法冲突。所以你可以定义它的样

    [Route("api/users/byname")]
    

    然后打电话

    http://localhost:50861/api/users/byname?FirstName=abc&LastName=xyz
    

    【讨论】:

    • 是的,你是对的。它与我的 getUsers 方法冲突。实际上,它不起作用,因为它只是返回 getusers 方法的值。有什么办法可以避免吗?
    • 用null替换空字符串并尝试
    • 感谢阿卜杜勒!这次有效。但是,它仅在我传入两个参数时才有效。我将名字和姓氏设置为空作为参数,所以如果没有传入,它们将为空?前任。如果我说 /api/users/byname?firstname=abc,lastname 参数是否为空?如果您需要,我已经更新了 getbyfirstname 方法。
    • @William 您的路由是查询字符串之前的 URL(URL 的查询字符串部分从 ? 开始)。似乎您想使用查询参数,而不是路由参数。像 Abdul 建议的路线 (/api/users/byname) 应该可以实现这一目标。
    • @John 没错,/api/users/byname 帮助我使用 GetByFirstName 方法。但是,如果我说 /api/users/byname/lastname=abc&firstname=def,它可以工作。如果我说 /api/users/byname/lastname=abc,它会返回 nothong。我想知道我上面的算法是否不正确。如果我将其写为 public IEnumerable GetByName(string FirstName = null, string LastName = null),我得到的变量 lastname 是 null?还是一片空白?
    猜你喜欢
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 2017-12-25
    • 1970-01-01
    相关资源
    最近更新 更多