how to test apiController

2019-07-23 04:53发布

Im working on an existing Windows Service project in VS 2013.

I've added a web API Controller class I cant remember now if its a (v2.1) or (v1) controller class....Anyway I've called it SynchroniseFromAwsService

Im trying to call it from a AWS lambda call but it is telling me I dont have access. So I need to test it locally to see if it is working to try and diagnose the issue.

I want to test this locally but am unsure how to do so..please see code...

 namespace Workflow
{
    public class SynchroniseFromAwsService: ApiController
    {
        //// POST api/<controller>
        public string SapCall([FromBody]string xmlFile)
        {
            string responseMsg = "Failed Import User";

            if (!IsNewestVersionOfXMLFile(xmlFile))
            {
                responseMsg = "Not latest version of file, update not performed";
            }
            else
            {
                Business.PersonnelReplicate personnelReplicate = BusinessLogic.SynchronisePersonnel.BuildFromDataContractXml<Business.PersonnelReplicate>(xmlFile);
                bool result = Service.Personnel.SynchroniseCache(personnelReplicate);

                if (result)
                {
                    responseMsg = "Success Import Sap Cache User";
                }
            }

            return "{\"response\" : \" " + responseMsg + " \" , \"isNewActiveDirectoryUser\" : \" false \"}";
        }
}
}

I've read on google to download a program called Postman and test it.

Is there nothing I can call in VS and just pass in a dummy data string containing an xml file to test?

What would be the best way

thank you for any replied

2条回答
Animai°情兽
2楼-- · 2019-07-23 05:02

you can use

RestSharp Simple REST and HTTP API Client for .NET

visit http://restsharp.org/ for more details.

using nuget package manager install this dependency in your project

Install-Package RestSharp -Version 106.2.0
查看更多
看我几分像从前
3楼-- · 2019-07-23 05:19

Your code is right. You need to test it locally is it. You can just accomplish it using Postman rest client itself. It's very simple.
In your code the HTTP method attribute is missing for your function SapCall. So, specify attribute for your method used to identify which type of request is this.

[HttpPost]
public string SapCall([FromBody]string xmlFile){

Now use postman rest client and call ur API url. It will get execute successfully.

查看更多
登录 后发表回答