0

I am trying to call a web API from my web application. I am using .Net 5.0 and while writing the code I am getting the error:

"The name 'JsonConvert' does not exist in the current "definition JsonConvert.DeserializeObject method.

So my question is what's the @inject... I need to use in order to use in razor page

@code {
    private Employee Employee{ get; set; } = new Employee();

    private async void HandleValidSubmit()
    {
        try
        {
            var response = await Http.PostAsJsonAsync("/api/Employee", Employee);
            response.EnsureSuccessStatusCode();

            var content = await response.Content.ReadAsStringAsync();
            var employee= JsonConvert.DeserializeObject<Employee>(content);
            Navigation.NavigateTo($"employee/edit/{employee.Id}");
        }
        catch (AccessTokenNotAvailableException exception)
        {
            exception.Redirect();
        }
        catch (Exception e)
        {
        }
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Newtonsoft is not included by default in Blazor/Wasm. You can add the package but you will need a good reason to add that much bloat for a _second_ Json API. Look at `System.Text.Json` first. – H H Jan 05 '22 at 07:00

2 Answers2

-1

Try to add the following code into your razor page:

@using Newtonsoft.Json;
Yiyi You
  • 16,875
  • 1
  • 10
  • 22
-1
@using Newtonsoft.Json;

string json = JsonConvert.SerializeObject(object);
var res = JsonConvert.DeserializeObject(json);
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jan 05 '22 at 00:19