Articles → ASP .NET WEB API → FromUri and FromBody in web API

FromUri and FromBody in web API






Purpose









Scenarios of using FromUri and FromBody











FromBody Example










  [System.Web.Http.HttpPost]
public void PassDataInBody([FromBody] string name)
{
            System.IO.File.WriteAllText(@"c:\test\from_body_text_file.txt", name);
}




string data = Console.ReadLine();
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:1482/");
var response = client.PostAsJsonAsync("api/Test", data).Result;



FromUri Example




[System.Web.Http.HttpGet]
public string GetSomething([FromUri]  string name)
{
            return "Hello " + name;
}




using (var client = new WebClient())
{
                client.Headers["request-type"] = "application/json";
                string result = client.DownloadString("http://localhost:1482/api/Test?name='karan'");
}



Posted By  -  Karan Gupta
 
Posted On  -  Saturday, January 07, 2017

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250