簡體   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. API 使用的是個人賬戶認證。

但是當我發送 API 調用時,它總是返回“False”。 我已經通過刪除[Authorized]屬性來檢查授權請求是否有問題,但我仍然得到同樣的錯誤。

他是我的 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);
        }
    }
}

在此處輸入圖像描述

任何人都可以幫助找出問題所在嗎?

如果您返回字符串嘗試

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