REST interface, server for GET,PUT,POST,DELETE

Noah Aas 360 Reputation points
2024-05-23T17:15:44.0466667+00:00

Hello,

My goal is to control a Rest Server from a WPF C# desktop app. Get, Push, Pop, Delete should be tested. The server does not require extensive logic, only test data. Later, the customer REST server is connected.

How can I achieve this? How should I proceed? Alternatively, there is PostMan, SoapUI. However, if anyone knows of a good tutorial, please let me know. The advantage would be that I could install it on any PC. Here I am dependent on the development PC. Not easier without experience.

Can you help? With a test project and a small guide. Thanks in advance.

At moment no connection, port is right.

 string url = "https://localhost:5029/api/endpoint";
 string url = "http://localhost:5029/api/endpoint";
 

//Open Visual Studio and created a new ASP.NET Core Web API project.

//GET   Read or retrieve data

//POST  Add new data

//PUT   Update data that already exists

//DELETE Remove data

enter image description here

enter image description here

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 = @"<Request><Data>Hello, World!</Data></Request>";

        // The URL to send the POST request to (update with your local server URL)
        string url = "https://localhost:5029/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);
        }
    }
}

// ** ASP.Core project
//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");
        }
    }
}
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,344 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,396 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,573 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
314 questions
{count} votes

Accepted answer
  1. AgaveJoe 27,421 Reputation points
    2024-05-30T12:12:35.15+00:00

    Maybe it's security settings from my company.

    It's not a security setting. The web applications are hosted on your machine. Plus, a 404 mean the client was able to access the remote web application because the web application returned a 404 to the client.

    One last screenshot. Do you see anything?

    A 404 simply means the URL was not found. The screenshot you shared is a web application not Web API. Did you add Web API support to an MVC or Razor Pages application? Can you share your route setup in the Startup.cs or Program.cs files? There are several different port numbers in your post. Are you sure port 44347 hosts the API?

    After reviewing your code, it seems to me the problems you are experiencing stem from not understanding fundamentals. I provided links to two Web API tutorials. I also provided a link on routing. Did you go through the tutorials? I'm asking because the tutorial cover the very aspects you are struggling with.

    Before you argue that the tutorials use JSON not XML, the data format does not matter. As already illustrated in this thread, serialization is a configuration. Web API handles JSON by default but can also handle only XML or JSON and XML at the same time.


2 additional answers

Sort by: Most helpful
  1. SurferOnWww 2,406 Reputation points
    2024-05-24T00:47:29+00:00

    string url = "https://localhost:5029/api/endpoint"; string url = "http://localhost:5029/api/endpoint";

    Are the above settings correct? It seems to me NOT.

    Your code of the controller shown in your question is as follows:

    [Route("api/[controller]")] [ApiController] public class TestController : ControllerBase

    Since you specify [Route("api/[controller]")] the url to the controller must be:

    https://localhost:xxxx/api/Test

    http://localhost:yyyy/api/Test

    As for the port numbers xxxxx and yyyyy, see the launchSettings.json file under the Properties folder.


  2. SurferOnWww 2,406 Reputation points
    2024-05-28T00:28:19.7033333+00:00

    The url shown in the image of your above reply:

    https://localhost:5029/api/test

    is not correct. Your launchSettings.json shows the ssl port as follows:

    • IIS Express: 44347
    • Kestrel: 7013