.Net MAUI 7,Consume API,Get和Get By ID方法都可以,但返回405,Method Not Allow for Post,Put和Delete

ghg1uchk  于 2023-04-07  发布在  .NET
关注(0)|答案(1)|浏览(114)

我有现有的web和MAUI的API控制器。从web客户端消费是好的,但与MAUI获取和获取的id方法是好的,但后,放和删除功能返回405错误。
无授权的Web控制器

//[Authorize]
        [Route("api/System/GetSsC09EVideoType")]
        [HttpGet]
        public IHttpActionResult GETSsC09EVideoType()
        {
            IList<SsC09EVideoType> entity = SsC09EVideoTypeModel.SaCLSsC09EVideoType();
            return Ok(entity);
        }
        [Route("api/System/GetSsC09EVideoTypeByID")]
        [HttpGet]
        public IHttpActionResult GETSsC09EVideoTypeByID(int SID)
        {
            SsC09EVideoType entity = SsC09EVideoTypeModel.ReCLSsC09EVideoType(SID);
            return Ok(entity);
        }
        [Route("api/System/PostSsC09EVideoType")]
        [HttpPost]
        public IHttpActionResult POSTSsC09EVideoType([FromBody] SsC09EVideoType mySsC09EVideoType)       
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            int count = SsC09EVideoTypeModel.InSsC09EVideoType(mySsC09EVideoType);
            return Ok(count);
        }
        [Route("api/System/PutSsC09EVideoType")]
        [HttpPut]
        public IHttpActionResult PUTSsC09EVideoType(int SID, [FromBody] SsC09EVideoType mySsC09EVideoType)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            bool isUpdate = SsC09EVideoTypeModel.UpSsC09EVideoType(mySsC09EVideoType, SID);
            return Ok(isUpdate);
        }
        [Route("api/System/DeleteSsC09EVideoType")]
        [HttpDelete]
        public IHttpActionResult DELETESsC09EVideoType(int SID)
        {
            bool isDelete = SsC09EVideoTypeModel.DeSsC09EVideoType(SID);
            return Ok(isDelete);
        }

Get by ID方法的工作代码

public static async Task<SsC09EVideoType> ReCLSsC09EVideoType(string myToken, int SID)
        {
            using (Global.client = new HttpClient())
            {
                Global.client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                Global.client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", myToken);
                Global.client.DefaultRequestHeaders.Add("APIKey", Global.APIKey);
                try
                {
                    SsC09EVideoType myobj = null;
                    HttpResponseMessage response = await Global.client.GetAsync(Global.BaseUrlAddress + "api/System/GetSsC09EVideoTypeByID?SID=" + SID);
                    if (response.IsSuccessStatusCode)
                    {
                        myobj = response.Content.ReadFromJsonAsync<SsC09EVideoType>().Result;
                        return myobj;
                    }
                    
                    return myobj;
                }
            }
          
        }

但是对于post和其他函数返回StatusCode:405,ReasonPhrase:'不允许的方法'

public static async Task<int> InSsC09EVideoType(string myToken, SsC09EVideoType myCl)
        {
            using (Global.client = new HttpClient())
            {
                Global.client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                Global.client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", myToken);
                Global.client.DefaultRequestHeaders.Add("APIKey", Global.APIKey);
                int sid = -1;
                try
                {                    
                    HttpResponseMessage response = await Global.client.PostAsJsonAsync(Global.BaseUrlAddress + "api/System/PostSsC09EVideoType", myCl);
                    if (response.IsSuccessStatusCode)
                    {
                        sid = response.Content.ReadFromJsonAsync<int>().Result;
                        return sid;
                    }                   
                    return sid;
                }               
            }           
        }

从MAUI 7文档中尝试如下,同样返回405。

public static async Task<int> InSsC09EVideoType(string myToken, SsC09EVideoType myCl)
     {
           using (Global.client = new HttpClient())
           {
                Global.client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                Global.client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", myToken);
                Global.client.DefaultRequestHeaders.Add("APIKey", Global.APIKey);

                Uri uri = new Uri(string.Format(Global.BaseUrlAddress + "api/System/PostSsC09EVideoType", string.Empty));
                JsonSerializerOptions _serializerOptions = new JsonSerializerOptions
                {
                    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                    WriteIndented = true
                };
                int sid = -1;
                try
                {
                    string json = System.Text.Json.JsonSerializer.Serialize<SsC09EVideoType>(myCl, _serializerOptions);
                    StringContent content = new StringContent(json, Encoding.UTF8, "application/json");                    
                    HttpResponseMessage response = await Global.client.PostAsync(uri,content);
                    return sid;
                }              
            }           
        }

使用另一个HttpRequestMessage进行测试也会返回405

Uri uri = new Uri(string.Format(Global.BaseUrlAddress + "api/System/PostSsC09EVideoType", string.Empty)); 
HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, uri);
message.Headers.Add("Accept", "application/json");
message.Headers.Add("APIKey", "123");
message.Content = JsonContent.Create<SsC09EVideoType>(myCl);

HttpResponseMessage response = await Global.client.SendAsync(message);

我正在测试与本地设备(Android 10.0-API 29).有问题与API密钥头.

sr4lhrrt

sr4lhrrt1#

解决了!因为url格式。我原来的格式https://www.example.com/api导致405方法不允许发布,放置和删除。通过改变为https://example.com/api解决一切。

相关问题