using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace WebApplicationGetPutPost.Pages { public class HelloController : Controller { // GET: HelloController public ActionResult Index() { return View(); } // GET: HelloController/Details/5 public ActionResult Details(int id) { return View(); } // GET: HelloController/Create public ActionResult Create() { return View(); } // POST: HelloController/Create [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(IFormCollection collection) { try { return RedirectToAction(nameof(Index)); } catch { return View(); } } // GET: HelloController/Edit/5 public ActionResult Edit(int id) { return View(); } // POST: HelloController/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(int id, IFormCollection collection) { try { return RedirectToAction(nameof(Index)); } catch { return View(); } } // GET: HelloController/Delete/5 public ActionResult Delete(int id) { return View(); } // POST: HelloController/Delete/5 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Delete(int id, IFormCollection collection) { try { return RedirectToAction(nameof(Index)); } catch { return View(); } } } } //using Microsoft.AspNetCore.Mvc; //using System.Xml.Linq; //namespace WebApplicationGetPutPost.Pages //{ // public class HomeController : Controller // { // public IActionResult Index() // { // return View(); // } // } //} 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"); } } } { "$schema": "http://json.schemastore.org/launchsettings.json", "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:58864", "sslPort": 44347 } }, "profiles": { "http": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "applicationUrl": "http://localhost:5029", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "https": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "applicationUrl": "https://localhost:7013;http://localhost:5029", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } } var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorPages(); var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapRazorPages(); app.Run(); var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddControllers().AddXmlDataContractSerializerFormatters(); var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapRazorPages(); app.Run(); using System; using System.Net.Http; using System.Text; using System.Threading.Tasks; internal class Program { static async Task Main(string[] args) { // Your XML string string xmlData = @"Hello, World!"; // The URL to send the POST request to (update with your local server URL) string url = "https://localhost:5029/api/test"; url = "https://localhost:44347/api/hello"; url = "https://localhost:7013/api/hello"; url = "https://localhost:44347/api/test"; url = "https://localhost:44347/api/test2"; // 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); } } }