首页 > 解决方案 > c# Web API 在使用 React Native Fetch Call 时在 Endpoint 调用 Post 未触发

问题描述

API 控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace LinkrWebApi.Controllers
{
public class ValuesController : ApiController
{
    // GET api/values
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/values/5
    [Route("api/values/{country}/{type}")]
    public string Get(string country, string type)
    {






        return "value";
    }

    public class GetResource
    {
        public string countryName { set; get; }
        public string imageLogo { set; get; }
        public string desc { set; get; }
        public string link { set; get; }
    }

    // POST api/values
    public void Post(string countryName)
    {
    }

    // PUT api/values/5
    public void Put(int id, [FromBody] string value)
    {
    }

    // DELETE api/values/5
    public void Delete(int id)
    {
    }
}
}

还有我的 Fetch 调用:

 fetch('https://localhost:44374/api/values', {
    method: 'post',
    body: "countryName=sc",
    headers: new Headers({
                    "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
                })
  }).then(function(response) {
    return response.json();
  }).then(function(data) {
  //navigation.navigate('FinalResource', { companyName: route.params.companyName, imageLogo: route.params.imageLogo, desc: txtD, link: txtL })

  });

但是我的 Web API 中的 Post 方法没有触发,get 工作但不是 post,任何人都可以看到错误吗?

标签: c#react-nativerestfetch

解决方案


推荐阅读