简体   繁体   English

ASP.NET MVC controller 在发布/获取请求时始终返回“False”

[英]ASP.NET MVC controller always returns "False" on Post/Get request

I'm facing a strange issue, I have created an ASP.NET Web API and added a new controller deriving from ApiController and added a new Test action. I'm facing a strange issue, I have created an ASP.NET Web API and added a new controller deriving from ApiController and added a new Test action. The API is using individual accounts authentication. API 使用的是个人账户认证。

But when I send an API call, it always returns "False".但是当我发送 API 调用时,它总是返回“False”。 I've checked by removing the [Authorized] attribute to check if there is something wrong with authorizing the request, but I still get the same error.我已经通过删除[Authorized]属性来检查授权请求是否有问题,但我仍然得到同样的错误。

He is my controller class:他是我的 controller class:

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 : 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);
        }
    }
}

在此处输入图像描述

Can anyone help to find out what the issue is?任何人都可以帮助找出问题所在吗?

If you return string try如果您返回字符串尝试

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

    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);
        }
    }
}

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

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