Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Thomsen 112 posts 335 karma points
    Sep 24, 2017 @ 21:24
    Thomsen
    0

    Async post to controller that returns JSon

    Hi

    This has been asked before in a previous unsolved thread from 2013, so I wonder if best practice has been figured out since then, or if any new comers could help me out.

    I am implementing my existing MVC project into Umbraco. In my existing project I have async ajax posts to my controllers using jQuery. Using Umbraco SurfaceController, the task will look like this:

      public class MySurfaceController: SurfaceController
        {
            [HttpPost]
            public JsonResult PostThatReturnsJSon(myModel model)
            {
    
              // Some logic here that involves 
              // HttpContext.Current.Session["SomeSessionVariable"];
    
                string message = string.Format("Successfully processed");
                return Json(new { Success = true, Message = message });
            }
        }
    

    This will not work, and by reading articles about Umbraco Surface Controllers, it is stated that Surface Controllers are only designed for "normal" form posts and for returning i.e. "CurrentUmbracoPage".

    Then there is the UmbracoApiController, but since an ApiController is stateless, it is not possible to use logic that involves sessions or cookies.

    I would be thankful if someone has figured out the solution for this – or a hint to a solution :-)

    Best regards

  • Tobias Klika 101 posts 570 karma points c-trib
    Sep 26, 2017 @ 13:09
    Tobias Klika
    100

    For sure you can use SurfaceController for an ajax POST?!

    You just need to post to /umbraco/surface/MySurface/PostThatReturnsJSon.

  • Thomsen 112 posts 335 karma points
    Sep 27, 2017 @ 14:05
    Thomsen
    0

    Thank you Tobias, I was missing the "/umbraco/" part of the path... :-O

    Thx.

Please Sign in or register to post replies

Write your reply to:

Draft