how to test apiController

ぃ、小莉子 提交于 2020-01-05 15:14:50

问题


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


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/48107646/how-to-test-apicontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!