Hi @Markus Freitag,
As per your requirement, you just want to create a very simple test, just refer to the getting started documentation.
When you create a new WebAPI, you will find get and POST methods in ValuesController.
You can follow the docs and set some test data according to the client so you don't need to connect to the database.
You also need to know how to call the Web API from a .NET client.
Call a Web API From a .NET Client (C#)
Below is the demo I wrote, you can refer to it.
WebAPI
public class ValuesController : ApiController
{
public class RequestWorkorder
{
public string Program { get; set; }
public string Workorder { get; set; }
}
RequestWorkorder[] requestWorkorders = new RequestWorkorder[]
{
new RequestWorkorder { Program = "1", Workorder = "a" },
new RequestWorkorder { Program = "2", Workorder = "b", },
new RequestWorkorder{ Program = "3", Workorder = "c"}
};
public IEnumerable<RequestWorkorder> GetAllProducts()
{
return requestWorkorders;
}
public IHttpActionResult GetProduct(string program)
{
var product = requestWorkorders.FirstOrDefault((p) => p.Program == program);
if (product == null)
{
return NotFound();
}
return Ok(product);
}
public IHttpActionResult Post([FromBody] RequestWorkorder requestWorkorder)
{
List<RequestWorkorder> requestWorkorders =new List<RequestWorkorder>();
requestWorkorders.Add(requestWorkorder);
return Ok();
}
}
.NET client
public class RequestWorkorder
{
public string Program { get; set; }
public string Workorder { get; set; }
}
RequestWorkorder p = new RequestWorkorder()
{
Program = "4",
Workorder = "D"
};
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://localhost:44377/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string authorization = $"{""}:{""}";
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "");
//GET Method
HttpResponseMessage response = await client.GetAsync("api/Values");
if (response.IsSuccessStatusCode)
{
List<RequestWorkorder> requestWorkorder = null;
requestWorkorder = await response.Content.ReadAsAsync<List<RequestWorkorder>>();
Console.WriteLine("Id:{0}\tName:{1}", requestWorkorder[0].Program, requestWorkorder[0].Workorder);
}
//POST Method
HttpResponseMessage response1 = await client.PostAsJsonAsync("api/Values", p);
if (response1.IsSuccessStatusCode)
{
Console.WriteLine("Success");
}
else
{
Console.WriteLine("Internal server Error");
}
}
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.