andy wilson
andy wilson

Reputation: 970

C# WebApi receive json from C# MVC through body

I am trying to get my C# MVC to send the data that the model gets as a JSON x-www-form-urlencoded to my web api if I post from postman to the web api it will work but I can't get it to send it through the MVC controller.

I'm trying to do it this way:

Controller:

public ActionResult Update(Models.CaseModel caseModel, int id)
{
    string date = caseModel.DOB.ToString("dd-MM-yyyy");
    string url = "http://10.0.2.31/testwebapi/api/case/UpdateCasePersonal/";

    var json = new JavaScriptSerializer().Serialize(caseModel);

    WebClient wc = new WebClient();

    var data = wc.UploadString(url, json);

    return Redirect(Url.Content("~/Case/Personal/" + id));
}

Model class:

public class CaseModel
{
    public int Caseid { get; set; }
    public string Title { get; set; }
    public string Forename { get; set; }
    public string Surname { get; set; }
    public string Postcode { get; set; }
    public string Telephone { get; set; }
    public string Email { get; set; }
    [DisplayFormat( ApplyFormatInEditMode = true, DataFormatString = "{0:dd'-'MM'-'yyyy}")]
    public DateTime DOB { get; set; }
    public string Mobile { get; set; }
    public string MaritalStatus { get; set; }
    public string LoanPurpose { get; set; }
}

C# Web Api:

[HttpPost]
[Route("UpdateCasePersonal/")]
public object UpdateCasePersonal([FromBody] PersonalModel personal)
{
    using (SqlConnection con = new SqlConnection(conString))
    {
        con.Open();

        var query = $@"UPDATE TestDB.dbo.[crm-data] SET Title=@Title, Forename=@Forename, Surname=@Surname, Telephone=@Telephone, Email=@Email, Mobile=@Mobile, DOB=@DOB, LoanPurpose=@Purpose, MaritalStatus=@Marital WHERE Caseid=" + personal.CaseID;

        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            cmd.Parameters.Add("@Title", SqlDbType.NVarChar, 50).Value = personal.Title;
            cmd.Parameters.Add("@Forename", SqlDbType.NVarChar, 50).Value = personal.Forename;
            cmd.Parameters.Add("@Surname", SqlDbType.NVarChar, 50).Value = personal.Surname;
            cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 50).Value = personal.Email;
            cmd.Parameters.Add("@Telephone", SqlDbType.NVarChar, 50).Value = personal.Telephone;
            cmd.Parameters.Add("@Mobile", SqlDbType.NVarChar, 50).Value = personal.Mobile;
            cmd.Parameters.Add("@DOB", SqlDbType.Date, 50).Value = personal.DOB;
            cmd.Parameters.Add("@Purpose", SqlDbType.NVarChar, 50).Value = personal.LoanPurpose;
            cmd.Parameters.Add("@Marital", SqlDbType.NVarChar, 50).Value = personal.Marital;

            cmd.CommandType = CommandType.Text;

            var dtb = new DataTable();
            var da = new SqlDataAdapter(cmd);
            da.Fill(dtb);

            return "Updated";
        }
    }
}

C# Web Api Model:

public class PersonalModel
{
    [JsonProperty("CaseID")]
    public string CaseID { get; set; }
    [JsonProperty("Title")]
    public string Title { get; set; }
    [JsonProperty("Forename")]
    public string Forename { get; set; }
    [JsonProperty("Surname")]
    public string Surname { get; set; }
    [JsonProperty("Postcode")]
    public string Postcode { get; set; }
    [JsonProperty("Telephone")]
    public string Telephone { get; set; }
    [JsonProperty("Email")]
    public string Email { get; set; }
    [JsonProperty("DOB")]
    public string DOB { get; set; }
    [JsonProperty("Mobile")]
    public string Mobile { get; set; }
    [JsonProperty("Marital")]
    public string Marital { get; set; }
    [JsonProperty("LoanPurpose")]
    public string LoanPurpose { get; set; }
}

I keep getting the error

The remote server returned an error: (415) Unsupported Media Type.

Why is this and how do I fix it?

Upvotes: 0

Views: 1715

Answers (1)

Daniel May
Daniel May

Reputation: 8246

It looks like you haven't set the Content-Type header on your WebClient.

wc.Headers.Add("Content-Type","x-www-form-urlencoded");

Upvotes: 1

Related Questions