1

I am using Sitecore 10 and trying to implement an API controller, but I got this issue when calling API in postman https://{mydomain}/saveform

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "Multiple actions were found that match the request: \r\nSaveForm on type ATLP.Feature.SaveSitecoreForm.Controllers.SaveSitecoreFormController\r\nSaveForm on type ATLP.Feature.SaveSitecoreForm.Controllers.SaveSitecoreFormController",
    "ExceptionType": "System.InvalidOperationException",
    "StackTrace": "   at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)\r\n   at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()"
}

this my controller

[ServicesController]
[RequiredApiKey]
public class SaveSitecoreFormController : ApiController
{
    private SaveFormRepository _repository;
    public SaveSitecoreFormController()
    {
        _repository = new SaveFormRepository();
    }

    [System.Web.Http.HttpPost]
    [Route("saveform")]
    public  IHttpActionResult SaveForm([FromBody]SitecoreFormDataModel formData)
    {
        List<FormFieldDataModel> FormFieldList = new List<FormFieldDataModel>();
        if (formData == null)
        {
            return BadRequest ("Missing form data");
        }
        else
        {
            var formId = formData.data.Where(f => f.key.Contains("FormItemId")).FirstOrDefault();
            Guid formGuid = new Guid(formId.value);
            var formEmailData = formData.data.Where(f => f.key.Contains("Email")).FirstOrDefault();
            if (formEmailData != null)
            {
                FormFieldDataModel emailFieldDataModel = new FormFieldDataModel
                {
                    FormFieldName = formEmailData.key,
                    FormFieldValue = formEmailData.value,
                    FormFieldGuid = new Guid("{B026F347-0CEE-4FF0-B435-E4737B253BDA}")
                };
                FormFieldList.Add(emailFieldDataModel);
                _repository.SaveFormData(formGuid, FormFieldList);
            }
            return Ok("Create CRM and Lead Objects Successful");
        }
    }
}

this serviceConfigurator class

public class ServicesConfigurator : IServicesConfigurator
{
    public void Configure(IServiceCollection serviceCollection)
    {
        serviceCollection.Replace(ServiceDescriptor.Transient(typeof(SaveSitecoreFormController),
            typeof(SaveSitecoreFormController)));
    }
}

and this config file

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
    <services>
        <configurator type="ATLP.Feature.SaveSitecoreForm.Services.ServicesConfigurator, ATLP.Feature.SaveSitecoreForm" />
    </services>
</sitecore>

any ideas what is missing here?

1 Answer 1

3

To create an apiController you need to follow next steps

1 . Create a class which inherits from ApiController

public class PersonApiController : ApiController
  1. You need to create your method which represent api call :

     [System.Web.Http.HttpGet]
     [Route("getvcard/{itemIdString}")]
     public HttpResponseMessage GetItemInformation(string itemIdString)
    
  2. You need to register the ApiController

    public class Register : IServicesConfigurator
         {
             public void Configure(IServiceCollection serviceCollection)
             {
    
                 serviceCollection.Replace(ServiceDescriptor.Transient(typeof(PersonApiController),
                     typeof(PersonApiController)));
             }
         }
    
  3. You need to register in the configuration :

    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
      <sitecore>
        <services>
          <configurator 
             type="yourNameSpace.ServicesConfigurator,yourNamespace" />
        </services>
       </sitecore>
     </configuration>
    
  4. To call the api you need to call it in this way

    https://yourhostname/getvarcad/itemid

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.