异步 post 到 Umbraco 中 returns JSon 的控制器

Async post to controller that returns JSon in Umbraco

我正在将我现有的 MVC 项目实施到 Umbraco 中。在我现有的项目中,我使用 jQuery 将异步 ajax 帖子发送到我的控制器。使用 Umbraco SurfaceController,任务将如下所示:

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 });
    }
}

这是行不通的,通过阅读有关 Umbraco Surface Controllers 的文章,表明 Surface Controllers 仅设计用于 "normal" 表单发布和返回,即 "CurrentUmbracoPage".

然后是 UmbracoApiController,但由于 ApiController 是无状态的,因此无法使用涉及会话或 cookie 的逻辑。

如果有人对此有解决方案或最佳实践的提示,我将不胜感激。

此致

我猜你使用了错误的 URL 来调用动作,因为使用 surfacecontroller 没问题,你使用它应该没有任何问题。在 Umbraco 中,如果你想调用控制器的动作,你应该使用这种格式的 Url:

http://yourwebsite.com/umbraco/surface/{controllername}/{action}/{id}

您还可以查看下面的link:https://our.umbraco.org/documentation/reference/routing/surface-controllers

在对 SurfaceController 进行异步调用时不能使用 UmbracoContext,但是,您的上述代码确实有效。您确定 URL 正确吗?

Surface 部分未从路线中删除,因此您的 URL 应该是: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);
    }