【问题标题】:Why does one Web API method work, whereas the other does not?为什么一种 Web API 方法有效,而另一种无效?
【发布时间】:2014-02-04 11:53:57
【问题描述】:

我的一种 Web API 方法运行良好,而另一种则完全不运行。

完美运行,我的意思是:

然而,另一个似乎甚至不知道自己。它通过以下方式回答浏览器请求:

他们两个的代码似乎设置相同,所以我不知道为什么一个像魅力一样工作而另一个失败。

相关代码为:

控制器

public class DepartmentsController : ApiController
{
    private readonly IDepartmentRepository _deptsRepository;

    public DepartmentsController(IDepartmentRepository deptsRepository)
    {
        if (deptsRepository == null)
        {
            throw new ArgumentNullException("deptsRepository is null");
        }
        _deptsRepository = deptsRepository;
    }

    [Route("api/Departments/Count")]
    public int GetCountOfDepartmentRecords()
    {
        return _deptsRepository.Get();
    }

    [Route("api/Departments")]
    public IEnumerable<Department> GetBatchOfDepartmentsByStartingID(int ID, int CountToFetch)
    {
        return _deptsRepository.Get(ID, CountToFetch);
    }

存储库

public class DepartmentRepository : IDepartmentRepository
{
    private readonly List<Department> departments = new List<Department>();

    public DepartmentRepository()
    {
        using (var conn = new OleDbConnection(
            @"Provider=Microsoft.ACE.OLEDB.12.0;User ID=Freebo;Password=RunningOnEmpty;Data Source=C:\CDBWin\DATA\CCRDAT42.MDB;Jet OLEDB:System database=C:\CDBWin\Data\nrbq.mdw"))
        {
            using (var cmd = conn.CreateCommand())
            {
                cmd.CommandText = "SELECT td_department_accounts.dept_no, IIF(ISNULL(t_accounts.name),'No Name provided',t_accounts.name) AS name FROM t_accounts INNER JOIN td_department_accounts ON t_accounts.account_no = td_department_accounts.account_no ORDER BY td_department_accounts.dept_no";
                cmd.CommandType = CommandType.Text;
                conn.Open();
                int i = 1;
                using (OleDbDataReader oleDbD8aReader = cmd.ExecuteReader())
                {
                    while (oleDbD8aReader != null && oleDbD8aReader.Read())
                    {
                        int deptNum = oleDbD8aReader.GetInt16(0);
                        string deptName = oleDbD8aReader.GetString(1);
                        Add(new Department { Id = i, AccountId = deptNum, Name = deptName });
                        i++;
                    }
                }
            }
        }
    }

    public int Get()
    {
        return departments.Count;
    }

    private Department Get(int ID) // called by Delete()
    {
        return departments.First(d => d.Id == ID);
    }

如果进入:

http://shannon2:28642/api/Departments/Count 

在浏览器工作执行Controller的GetCountOfDepartmentRecords()方法,为什么输入:

 http://localhost:28642/api/Departments/5/6

(或:

 http://localhost:28642/api/Departments/1/5

etc) 无法执行 Controller 的 GetBatchOfDepartmentsByStartingID() 方法?

【问题讨论】:

    标签: asp.net-mvc asp.net-web-api controller repository asp.net-web-api-routing


    【解决方案1】:

    您的路线缺少其参数。

    [Route("api/Departments/{ID:int}/{CountToFetch:int}")]
    

    【讨论】:

      【解决方案2】:

      此问题与您在下面的其他问题相似:

      Why is my Web API call returning "No action was found on the controller 'DPlatypus' that matches the request"?

      如果您希望这些值来自 url 的非查询字符串部分,则需要在路由模板中定义它们。所以,应该是

      [Route("api/Departments/{id}/{countToFetch}")]
      

      以下是一篇关于 Web API 中路由和操作选择的好文章:
      http://www.asp.net/web-api/overview/web-api-routing-and-actions

      【讨论】:

      • 谢谢;我可能之前说过*,但如果不是因为记性不好,我根本就没有记忆(*记不清了)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 1970-01-01
      • 2012-03-05
      • 2022-08-10
      • 2020-10-25
      相关资源
      最近更新 更多