1

I'm not getting proper response as per below return value. Currently i'm getting response as namespace of Project as : Feature.SitecoreAPi.Platform.Controller.TestAPIController+HttpActionResult. Expected response of my code should be Status Code: 200 & OK message 100 (for eg response should be: status code & string message).

This code will work fine, if i use APiController but here i have to use SitecoreController

public class TestAPIController :SitecoreController
    {
        [HttpGet]
        public IHttpActionResult TestResponse(int id)
        {
            id = 100;

            return new HttpActionResult(HttpStatusCode.OK, "OK message" + id);
        }


        public class HttpActionResult : IHttpActionResult
        {
            private readonly string _message;
            private readonly HttpStatusCode _statusCode;

            public HttpActionResult(HttpStatusCode statusCode, string message)
            {
                _statusCode = statusCode;
                _message = message;
            }

            public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
            {
                HttpResponseMessage response = new HttpResponseMessage(_statusCode)
                {
                    Content = new StringContent(_message)
                };
                return Task.FromResult(response);
            }
        }
4
  • What error you are getting, can you mention it in your question? Commented Jan 12, 2023 at 7:04
  • getting error to install the package "Microsoft.AspNet.WebApi.Client" but after installation its not working still getting red marked under 'CreateResponse' while using 'SitecoreController'. I have also tried to achieve this by 'IHttpActionResult' but in this case i'm getting response as 'Feature.SitecoreAPi.Platform.Controller.TestAPIController+HttpActionResult' instead of error message and status code for eg - 'return new HttpActionResult(HttpStatusCode.InternalServerError, "error message"); '
    – Ranjeet
    Commented Jan 12, 2023 at 7:26
  • Explain in your question what is the expected result and what is the actual result. We will reopen your question when it contains all the details allowing to help you.
    – Marek Musielak
    Commented Jan 12, 2023 at 7:34
  • @MarekMusielak I have updated as per your suggestion. Please reopen
    – Ranjeet
    Commented Jan 12, 2023 at 9:42

1 Answer 1

0

Using the same code I am getting the proper response. Here is my controller and Action.

using System.Net;
using Sitecore.Mvc.Controllers;
using System.Web.Http;
using System.Threading.Tasks;
using System.Net.Http;
using System.Threading;

namespace Test.Controllers
{
    public class TestController : SitecoreController
    {
        [System.Web.Http.HttpPost]
        public IHttpActionResult TestTransaction(string transactionId, string emailId)
        {
           var it = Sitecore.Data.Database.GetDatabase("master").GetItem("{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}");
            var result = new HttpActionResult(HttpStatusCode.OK, "OK message" + it);

            return result;
        }
    }

    public class HttpActionResult : IHttpActionResult
    {
        private readonly string _message;
        private readonly HttpStatusCode _statusCode;

        public HttpActionResult(HttpStatusCode statusCode, string message)
        {
            _statusCode = statusCode;
            _message = message;
        }

        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            HttpResponseMessage response = new HttpResponseMessage(_statusCode)
            {
                Content = new StringContent(_message)
            };
            return Task.FromResult(response);
        }
    }
}

And here is the result I am getting.

enter image description here

I am using Sitecore.NET 10.2.0 (rev. 006766) with .NET Framework 4.8. Here is the JS code I am using to call this function.

var email = $w("input[type='email']").val();
$w.ajax({
     url: "/ajax/Test/TestTransaction",
     type: "Post",
     async: false,
     data: { transactionId: JSON.stringify(transactionId), emailId: JSON.stringify(email) },
     success: function (data) {
      }
});

I would suggest you use the same code with the namespaces I used and check if still you get this issue.

Thanks

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.