Click here to Skip to main content
15,881,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
in my controller:

 public class ShoppingCartController : Controller
    {
         
        // GET: /ShoppingCart/
        public ActionResult Index()
        {            
            return View(this.HttpContext);
        }
...
}


in view:i try to send the httpcontext to api method as following.
var urll = 'https://localhost:44379/api/apiClass/IndexOfCarts';
$.ajax({
            type: "post",
            url: urll, error: function () { alert("error carts") },
               @*data: JSON.stringify(@Model.HttpContext, null, '\t'),*@
         data: { httpContext:  @Model},
        //    contentType: 'application/json',
        //dataType:"json",
            success: function (dataa) {
                alert("****ok carts****")

            }
        });



in api action:

[Microsoft.AspNetCore.Mvc.HttpPost()]
      public ActionResult<string> IndexOfCarts([FromForm]HttpContext httpContext)
      {


          return "ddd";
      }

the api action dont call.but when change the parameter to another types and use ajax to request to api action, the api action called and work

What I have tried:

i change[fromform] to [frombody] for parameter,but also the api not called.(The program does not go to the api action)
Posted
Updated 2-Nov-20 3:23am
Comments
F-ES Sitecore 2-Nov-20 8:19am    
You can't do that, you can only pass text as json parameters, not objects in memory. To send an object you need to serialise it to text, send it via ajax, then deserialise it in your api. The httpcontext doesn't let you do this by default. Create a serialisable object that contains just the bits of info from httpcontext that you need and send that as your parameters.

1 solution

The HttpContext object represents the current request and response. It makes absolutely no sense to pass it to another request.

Instead, extract the relevant parameters you want to pass to the new request, and send those values via a POST or GET request.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900