skip to Main Content

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

enter image description here

Can anyone help to find out what the issue is?

2

Answers


  1. If you return string try

    public string test(string name) 
    {
      return Ok(name);
    }
    
    
    Login or Signup to reply.
  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);
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search