I have the following code:
[HttpPost]
[Route("createRepo")]
public HttpResponseMessage createRepo(GitHub githubInfo)
{
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(githubInfo.RepoName, System.Text.Encoding.UTF8, "application/json")
};
}
Very simply above, I have a POST route that requires a Github
object as input, and just returns its repoName
as provided in the object.
Here is the Github
model class:
public class GitHub {
public string RepoName { get; set; }
public string Organization { get; set; }
public GitHub(string RepoName, string Organization) {
this.RepoName = RepoName;
this.Organization = Organization;
}
}
Now, doing a POST request with form body returns an error:
It means that githubInfo
is null, and so you cannot access its property called RepoName
.
However, if I add in the following line to my model GitHub
class:
public GitHub() { }
Making the entire model:
public class GitHub {
public string RepoName { get; set; }
public string Organization { get; set; }
public GitHub(string RepoName, string Organization) {
this.RepoName = RepoName;
this.Organization = Organization;
}
public GitHub() { }
}
Then I have a different story:
It actually recognizes the input, and is able to print out the property name. Why? What is the significance of adding this empty constructor?