Hi @Noah Aas , Welcome to Microsoft Q&A,
To set up a mock server to test the interface and receive predefined XML responses, you can use ASP.NET Core to Create a Mock Server. It's simpler than SoapUI or Postman for creating a mock server.
- Create a New ASP.NET Core Web Application:
- Open Visual Studio and create a new ASP.NET Core Web API project.
- Define the Controller:
- Add a controller to handle incoming requests and send predefined XML responses.
using Microsoft.AspNetCore.Mvc; using System.Xml.Linq; namespace MockServer.Controllers { [Route("api/[controller]")] [ApiController] public class TestController : ControllerBase { [HttpPost] public IActionResult Post([FromBody] XElement request) { // Define the response based on some logic or input request XElement response = new XElement("Response", new XElement("Data", "Answer"), new XElement("Error", new XAttribute("code", "0"), new XAttribute("description", "No power")) ); return Content(response.ToString(), "application/xml"); } } }
- Run the Server:
- Build and run the project. This will host your mock server locally, typically on
https://localhost:5001
or similar.
- Build and run the project. This will host your mock server locally, typically on
Testing with the C# Client
Here’s the C# client code to send a request to your mock server and receive the predefined response:
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
// Your XML string
string xmlData = @"<Request><Data>Hello, World!</Data></Request>";
// The URL to send the POST request to (update with your local server URL)
string url = "https://localhost:5001/api/endpoint";
// Create a new HttpClient instance
using (HttpClient client = new HttpClient())
{
// Set the content type to XML
HttpContent content = new StringContent(xmlData, Encoding.UTF8, "application/xml");
// Send the POST request
HttpResponseMessage response = await client.PostAsync(url, content);
// Ensure the response was successful, or throw an exception
response.EnsureSuccessStatusCode();
// Read the response content
string responseContent = await response.Content.ReadAsStringAsync();
// Print the response
Console.WriteLine(responseContent);
}
}
}
Using ASP.NET Core to create a mock server can be an easier and more integrated solution for .NET developers compared to using SoapUI or Postman.
Best Regards,
Jiale
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.