【问题标题】:ASP.NET MVC controller always returns "False" on Post/Get requestASP.NET MVC 控制器在发布/获取请求时始终返回“False”
【发布时间】:2022-01-15 00:25:07
【问题描述】:

我遇到了一个奇怪的问题,我创建了一个 ASP.NET Web API 并添加了一个从 ApiController 派生的新控制器并添加了一个新的 Test 操作。 API 正在使用个人帐户身份验证。

但是当我发送 API 调用时,它总是返回“False”。我已通过删除[Authorized] 属性进行检查,以检查授权请求是否有问题,但我仍然收到相同的错误。

他是我的控制器类:

using System.Configuration;
using System.Web.Http;

namespace Healthterest.Controllers
{
    public class PinsController : ApiController
    {  
        public PinsController():base()
        {
        }

        // [Authorize]
        [HttpPost]
        [ActionName("test")]
        public IHttpActionResult test(string name) 
        {
            return Ok(name);
        }
    }
}

WebApiConfig:

using System;
using System.Web.Http;
using System.Web.Http.Cors;
using Microsoft.Owin.Security.OAuth;

namespace Healthterest
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Configure Web API to use only bearer token authentication.
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
            //EnableCrossSiteRequests(config);
            // Web API routes
            config.MapHttpAttributeRoutes();
            //config.EnableCors(new EnableCorsAttribute("*", headers: "*", methods: "*"));
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }

        private static void EnableCrossSiteRequests(HttpConfiguration config)
        {
            var cors = new EnableCorsAttribute(
                origins: "*",
                headers: "*",
                methods: "*");
            config.EnableCors(cors);
        }
    }
}

谁能帮忙找出问题所在?

【问题讨论】:

  • 为什么要把参数放在表单数据中?尝试发送您的参数是 Postman 中的原始 JSON 字符串。您还应该在 API 方法中放置断点,以查看它是否甚至被 Postman 调用。
  • @RahulSharma 即使使用原始 JSON 字符串也无法正常工作,而且断点也没有命中。
  • 如果你的断点没有被命中,这意味着你的配置有问题。尝试注释 config.SuppressDefaultHostAuthentication();config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); 行并尝试调用该方法。

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


【解决方案1】:

如果你返回字符串试试

public string test(string name) 
{
  return Ok(name);
}

【讨论】:

  • 嗨,它是如何工作的?它将通过 System.Web.Http.Results.OkNegotiatedContentResult 类型的运行时错误到 st​​ring
  • 去掉 OK 并返回 Just return name;
  • 不走运,我也检查了返回字符串。
【解决方案2】:
using System.Configuration;
using System.Web.Http;

namespace Healthterest.Controllers
{
    public class PinsController : ApiController
    {  
        public PinsController():base()
        {
        }

        // [Authorize]
        [HttpPost]
        [ActionName("test")]
        public IHttpActionResult test([FromForm]string name) 
        {
            return Ok(name);
        }
    }
}

【讨论】:

    猜你喜欢
    • 2018-07-14
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多