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 a hint to a solution or best practice for this.
Best regards
The UmbracoContext can't be used when making async calls to SurfaceControllers, however, your above code does work. Are you sure you're getting the URL right?
The Surface part is not removed from the route, so your URL should be: http://localhost:58473/umbraco/surface/mysurface/postthatreturnsjson
public JsonResult PostThatReturnsJSon()
{
// Some logic here that involves
// HttpContext.Current.Session["SomeSessionVariable"];
string message = string.Format("Successfully processed");
return Json(new { Success = true, Message = message }, JsonRequestBehavior.AllowGet);
}