.net核心的CRUD操作

时间:2019-09-26 22:18:39

标签: c# .net post crud core

我是C#的新手。 我正在尝试开发使用在服务器上上传的Web Api的多平台应用程序。

以下是GET / POST的代码:

public async void post_async(object sender, System.EventArgs e)
    {       

        Console.WriteLine("POST");


        try
        {
            HttpClient httpClient = new HttpClient();

            var uri = new Uri("https://mywebsite");

            var item = new users_model { username = "prove_username", email = "prove_mail", password = "prove_pwd", ranking = "prove_ranking"};


            var json = JsonConvert.SerializeObject(item, Formatting.Indented);
            var content = new StringContent(json, Encoding.UTF8, "application/json");
            var httpContent = new StringContent(json);

            var response = await httpClient.PostAsync(uri, httpContent);
            var message = await response.Content.ReadAsStringAsync();

            Console.WriteLine($"STATUS CODE POST:    " + response.StatusCode);
            Console.WriteLine($"REQUEST MESSAGE POST:    " + response.RequestMessage);
            Console.WriteLine($"CONTENT POST:    " + message);
            //Console.WriteLine($"DATA POST:    " + item.username);


        }
        catch (Exception er)
        {
            Console.WriteLine($"ERROR: " + er.ToString());

        }


    }

    async void get_async(object sender, System.EventArgs e)
    {

        try
        {
            HttpClient httpClient = new HttpClient();

            var uri = new Uri("https://mywebsite");
            var response = await httpClient.GetAsync(uri);
            var message = await response.Content.ReadAsStringAsync();

            Console.WriteLine($"STATUS CODE GET:    " + response.StatusCode);
            Console.WriteLine($"REQUEST MESSAGE GET:    " + response.RequestMessage);
            Console.WriteLine($"CONTENT GET:    " + message);


        }
        catch (Exception er)
        {
            Console.WriteLine($"ERROR: " + er.ToString());

        }

    }

这是控制器的代码:

public class users_model
{
    public string username { get; set; }
    public string email { get; set; }
    public string password { get; set; }
    public string ranking { get; set; }
}

这是Web Api的代码:

    [HttpGet]
    public async Task<List<users_model>> getAllUsers ()
    {

        FirebaseClient firebase = new FirebaseClient("https://myFirebaseSite/");

        Items = new List<users_model>();
        Items = (await firebase.Child("users").OnceAsync<users_model>()).Select(item => new users_model

        {
            username = item.Object.username,
            email = item.Object.email,
            password = item.Object.password,
            ranking = item.Object.ranking

        }).ToList();

        return Items;
    }

   [HttpPost]
   public IActionResult addUser([FromBody] users_model new_user)
        {

            return Ok($"post ok. username: " + new_user.username);
        }

如果我打电话给get_async =>没问题 如果我调用post_async,则会收到“ NotFound”。 如果我使用[ActionName(“ void_name”)]而不是[HttpPost]和[HttpGet],则会得到“ UnsupportedMediaType”

我什至尝试使用NUGet“ RestSharp”,但遇到了同样的问题。 请帮助我,这是我找不到解决方法的三天:(

0 个答案:

没有答案