我有现有的web和MAUI api控制器。从web客户端消费是可以的,但是使用MAUI的Get和Get by id方法是可以的,但是Post, Put和Delete函数返回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: 'Method Not Allowed'
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密钥头有问题。
<代码>代码>
解决!因为url格式。我原来的格式https://www.example.com/api造成405方法不允许张贴,放置和删除。通过改变https://example.com/api解决一切