【问题标题】:Debugging Web API returns Resource no match error调试 Web API 返回资源不匹配错误
【发布时间】:2018-08-18 05:28:46
【问题描述】:

当我尝试通过调用来调试应用程序时

  http://localhost:5/api/GetEmployeesDEV

它返回类似的错误

No HTTP resource was found that matches the request URI 'http://localhost:57764/api/GetEmployeesDEV'. No action was found on the controller 'GetEmployeesDEV' that matches the request.

调用 Odata 端点并返回调用收到的响应的 ASP.NET Web API。我有下面的控制器代码

 public class GetEmployeesDEVController : ApiController
 {
 [HttpGet]
 private async Task<EmployeeDTO.RootObject> Get()
{
    string userName_Core = ConfigurationManager.AppSettings["core_Username"];
    string password_Core = ConfigurationManager.AppSettings["core_Password"];
    string BaseURL_Core = ConfigurationManager.AppSettings["BaseURL_Core"];
    var byteArray_Core = Encoding.ASCII.GetBytes(userName_Core + ":" + password_Core);
    EmployeeDTO.RootObject returnObj = new EmployeeDTO.RootObject();

    try
    {
        // GET
        using (var client_Core = new HttpClient())
        {
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
            Uri uri = new Uri(BaseURL_Core);
            client_Core.BaseAddress = uri;
            client_Core.DefaultRequestHeaders.Accept.Clear();
            client_Core.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client_Core.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray_Core));

            string core_URL = BaseURL_Core;
            var response = client_Core.GetAsync(core_URL).Result;

            var responsedata = await response.Content.ReadAsStringAsync();
            returnObj = JsonConvert.DeserializeObject<EmployeeDTO.RootObject>(responsedata);
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    return returnObj;
}

不确定我是否在这里遗漏了什么。我没有对 RouteConfig 文件进行任何更改

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-web-api


    【解决方案1】:

    请更改函数范围

    来自私人

    private async Task<EmployeeDTO.RootObject> Get() 
    

    公众

    public async Task<EmployeeDTO.RootObject> Get()
    

    操作需要公开才能用于路由。

    【讨论】:

      猜你喜欢
      • 2019-06-21
      • 2014-07-17
      • 1970-01-01
      • 2014-12-15
      • 2014-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-21
      相关资源
      最近更新 更多