REST interface, C#

Markus Freitag 3,786 Reputation points
2022-06-10T09:44:19.413+00:00

Hello,
I need to send this data via a REST interface.

{  
  "actorname": "Marking",  
  "taskname": "2022-06-10-O424",  
  "stepname": "EXECUTION_2",  
  "parameters": [  
    { "name": "parameter A", "value": 73.1 },  
    { "name": "parameter B", "value": 12 }  
	{ "name": "parameter C", "value": 173.1 },  
    { "name": "parameter D", "value": 112 }  
	{ "name": "parameter E", "value": 273.1 },  
    { "name": "parameter F", "value": 312 }  
  ]  
}  

How can I achieve this?
How can I test this?

I think I have to send it by POST, because I have data with a relationship in it. 1 to n link.
What is the best way to achieve the goal?

GET is, I ask for infos.
POST, I must send a object, structure...
PUT, when must I need PUT?

Is there a test server where I can see what I have sent, where I can generate test messages.

Thanks for your help in advance.
Best regards

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,254 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,245 questions
{count} votes

Accepted answer
  1. AgaveJoe 26,201 Reputation points
    2022-06-20T11:52:31.617+00:00

    It is not a web application. It is a C# desktop application.

    Then fix the tags! You tagged this post as MVC and Web Forms which are web applications. Plus a REST service is a web application!

    The questions, what is the best way to create a server? A mock server?

    This information is covered above with example code and links. Can you explain why you are unable to perform these very basic tasks?

    My machine connects to a SCADA MES system.

    If you need help with communicating with a SCADA MES system then SCADA MES is a better place to get support.

    1 person found this answer helpful.

4 additional answers

Sort by: Most helpful
  1. AgaveJoe 26,201 Reputation points
    2022-06-22T10:47:38.777+00:00

    What is the best way to debug this?

    In visual Studio right click on the solution file and select properties. From there you can select multiple startup projects. I usually select "Current selection". Then click the web api project and press ctrl-F5 to start web api without the debugger. Next select the client app and run.

    for debugging the web api app I'll click run then use PostMan to make the request. Frankly, you have a lot of posts and should take time to learn how to use Visual Studio.

    How can I get this to run mode on another PC?

    I'm not sure what "run mode" is or what you're asking. Just copy the entire solution to another PC and open the solution in Visual Studio.

    If you are asking how to deploy the web api which is another Visual Studio question. Right click the Web API project, select publsihed, to a folder, enter the path, click finish. I used the path C:\inetpub\wwwroot\WebApiBasic. You'll surly have to create the WebApiBasic folder. finally publish the Web APi project. From here you can copy the deployed web api to another computer if needed.

    C:\inetpub\wwwroot\ is the local IIS instance if you have IIS installed. Open InetMgr.exe to configure the web application. You'll find WebApiBasic under the "Default Web Site". Right click WebApiBasic and select "Convert to Application" - > ok. Under Manage application on the right click browse to open the Web API in a browser.

    If you can still show the authorization, it would be great!

    Once the you have Web API deployed to IIS you can configure basic authentication in IIS under the authentication feature. Note: you must create a user account on the machine. You can figure this out on your own.

    Is there anything else you can say?

    I'm not sure what you are asking.

    I call then like this, right?

    I added Get(id), Post, Put and Delete methods to the project.

    I think it is import to point out that any beginning level Web API tutorial covers this stuff. Secondly, your customer must provide information on how the their service works. It is practically impossible build a client without the service documentation. You've mentioned that your customer does not supply this information which a very strange if they are actually your customer and paying you for development.

    1 person found this answer helpful.

  2. Bruce (SqlWork.com) 55,686 Reputation points
    2022-06-10T16:24:33.957+00:00

    in REST

    GET is used to fetch entity via a url query string
    POST is used add/create an entity
    PUT is used to update/replace an entity
    PATCH is used for partial updates
    DELETE is used to delete an entity

    those are just the methods. the api should define what the body data should be and any required headers.


  3. Zhi Lv - MSFT 32,011 Reputation points Microsoft Vendor
    2022-06-13T07:50:17.3+00:00

    Hi @Markus Freitag ,

    How can I achieve this?
    How can I test this?

    If you are using Asp.net core API, you can create an Asp.net core Web API project, by default in asp.net 6, it has configured with Swagger, then after creating the API controller, you could use the swagger UI to test the API methods.

    I think I have to send it by POST, because I have data with a relationship in it. 1 to n link.
    What is the best way to achieve the goal?

    To create the API or Rest Service, first, you need to create model based on the JSON data, you can search "json to c# class online" using Google or Bing, there have some online tools to generate the C# class based on the JSON data. And the Model like this:

    //required using System.ComponentModel.DataAnnotations;  
    public class TodoModel  
    {  
        [Key]  
        public int TodoId { get; set; }  
        public string actorname { get; set; }  
        public string taskname { get; set; }  
        public string stepname { get; set; }  
        public List<Parameter> parameters { get; set; }  
    }  
    public class Parameter  
    {  
        [Key]  
        public int PId { get; set; }  
        public string name { get; set; }  
        public double value { get; set; }  
    }  
    

    Then, right click the Controllers folder-> Add-> New Scaffolded Item.. , in the Popup window, Select API -> API Controller with actions, using Entity Framework. Finally, click the Add button:

    210781-image.png

    After that, it will generate the Controller with the CRUD and Put methods:

    210765-image.png

    Then, you can enable migration to generate the database and the table.

    PUT, when must I need PUT?

    The Put method is used to update the entity. Refer Web API Overview

    210715-image.png

    Running the application, and test the API method using Swagger UI:

    210647-2.gif

    Reference:

    first-web-api/samples

    Tutorial: Get started with EF Core in an ASP.NET MVC web app(This is similar in the API project)


    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.

    Best regards,
    Dillion


  4. Zhi Lv - MSFT 32,011 Reputation points Microsoft Vendor
    2022-06-14T05:27:24.38+00:00

    Hi @Markus Freitag ,

    Can you please still publish how you can configure the test answers under Postman?
    Would be very nice of you. Is this freeware?
    Questions about C#, answers via Postman.

    I don't have Visual Studio 2022. I need to develop it under VS2019. Can you please help me with this.
    GET and Post. Message.

    Can you upload the project? I must have done it once. It's difficult if you've never done it before. Thanks for your support.

    Since I don't have VS 2019 on my machine, I will create a .Net 4.7.2 API application using VS 2022, the steps and code should work well on VS 2019.

    1. Open VS, in the Create a new Project window, use the ASP.NET Web Application (.NET Framework) template to create a Web API project. 211105-image.png
    2. In the Web API project, in the Models folder, add the following code:
      public class TodoModel  
      {  
          [Key]  
          public int TodoId { get; set; }  
          public string actorname { get; set; }  
          public string taskname { get; set; }  
          public string stepname { get; set; }  
          public List<Parameter> parameters { get; set; }  
      }  
      public class Parameter  
      {  
          [Key]  
          public int PId { get; set; }  
          public string name { get; set; }  
          public double value { get; set; }  
      }  
      
    3. Right click the Controllers folder, select Add -> Controller or New Scaffolded Item 211027-image.png In the Add New Scaffolded Item window, select the API controller as below: 211095-image.png Then, select the model and Dbcontext (if there doesn't have a dbcontext, you can click the plus icon to add a new one) 211124-image.png [Note] After clicking the Add button, it might need to install some package to auto generate the relate controller. If meet any error, retry this steps again or based on the error message to install the package by yourself. Then the result as below: the controller has the CRUD and Put method.
      211077-image.png
    4. Runing the application, the result as below:
      211096-image.png
    5. Open Postman, according the application url and the API route to send the JSON data, like this:
      [Note] When using Postman, the method we need to select Post, and to transfer JSON data, in the request body, we need to choose raw and JSON: 211132-image.png The finally result as below:
      211085-2.gif

    You could view the source code from here:

    211142-todomodel.txt, 211048-todocontroller.txt, 211151-apidbcontext.txt


    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.

    Best regards,
    Dillion