简体   繁体   中英

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. The API is using individual accounts authentication.

But when I send an API call, it always returns "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.

He is my 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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