简体   繁体   English

Web API中的C#在HttpPost上找不到404

[英]404 not found on HttpPost from C# in Web API

I am receiving a 404: not found error when I try to call HasAccess. 我在尝试调用HasAccess时收到404:找不到错误。 I use HttpPost to be able to pass a User class because it contains / and \\ symbols in some of the strings. 我使用HttpPost能够传递User类,因为它在某些字符串中包含/和\\符号。 This worked as a get but was unable to function because of the extra slashes, so I need to figure out how to make it work as a post or find another way around the \\ and / restriction. 这样做虽然可以,但是由于多余的斜杠而无法正常工作,因此我需要弄清楚如何使其成为帖子,或者找到另一种绕\\和/限制的方法。 Here's the response message: 这是响应消息:

{Method: POST, RequestUri: 'http://localhost/WebServices/Security/api/SecurityCheck/HasAccess/',
Version: 1.1,
Content: System.Net.Http.ObjectContent`1[[Authorization.Models.User, Authorization, Version=1.0.3.0, Culture=neutral, PublicKeyToken=null]],
Headers:
{
  Accept: application/json
  Content-Type: application/json; charset=utf-8
  Content-Length: 118
}}

Here is the function declaration in Web API 这是Web API中的函数声明

[Route("api/SecurityCheck/HasAccess/")]
[HttpPost]
public bool HasAccess(User current)
{
  if (ValidateUser(current))
  {
    CurrentUser = current;

    return AzMan.Roles.Count != 0;
  }
  return false;
}

My Route Configuration in Global.asax Global.asax中的我的路线配置

  GlobalConfiguration.Configure(config =>
  {
    config.MapHttpAttributeRoutes();

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

and my call to the Web API function from C#: 和我从C#对Web API函数的调用:

public async Task executeAsyncPost(string method, int? operationID = null)
{
  try
  {
    using (var client = new HttpClient())
    {
      client.BaseAddress = new Uri(ConfigurationSettings.AppSettings["SecurityCheckAPI"]);
      client.DefaultRequestHeaders.Accept.Clear();
      client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

      var user = new User();
      user.UserName = User;
      user.Application = Application;
      user.AuthorizationStore = AuthorizationStore;

      string message = "api/SecurityCheck/" + method + "/";
      if (operationID.HasValue)
      {
        message += operationID.Value.ToString() + "/";
      }

      HttpResponseMessage response = await client.PostAsJsonAsync(message, user);

      if(response.IsSuccessStatusCode)
      {
        switch(method)
        {
          case "GetOperationIds":
            _operationIds = await response.Content.ReadAsAsync<int[]>();
            break;
          case "GetOperationNames":
            _operationNames = await response.Content.ReadAsAsync<string[]>();
            break;
          case "CheckAccess":
            _checkAccess = await response.Content.ReadAsAsync<bool>();
            break;
          case "HasAccess":
            _hasAccess = await response.Content.ReadAsAsync<bool>();
            break;
          case "GetRoles":
            _roles = await response.Content.ReadAsAsync<object[]>();
            break;
        }
      }
    }
  }
  catch (Exception ex)
  {
    throw ex;
  }
}

删除[Route("api/SecurityCheck/HasAccess/")]上的最后一个/以及RequestUri本身。

It was missing a / at the end of the request Uri. 在请求Uri的末尾缺少/。 The lack of that / actually removed the entire authorization section from the path and led directly into api. 缺少那个/实际上从路径中删除了整个授权部分,并直接进入api。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM