【问题标题】:Async post to controller that returns JSon in Umbraco异步发布到控制器,在 Umbraco 中返回 JSon
【发布时间】:2018-03-06 18:52:34
【问题描述】:

我正在将我现有的 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 控制器的文章,据说 Surface 控制器仅适用于“正常”表单帖子和返回,即“CurrentUmbracoPage”。

然后是 UmbracoApiController,但是由于 ApiController 是无状态的,所以不可能使用涉及会话或 cookie 的逻辑。

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

最好的问候

【问题讨论】:

    标签: asp.net ajax asp.net-mvc-4 umbraco umbraco7


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢 Mikkel - 如上所述,我在从 jQuery 调用控制器时缺少路径的 /umbraco/ 部分。
    【解决方案2】:

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

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

    您也可以查看以下链接:https://our.umbraco.org/documentation/reference/routing/surface-controllers

    【讨论】:

    • 谢谢...我只是错过了路径的 /umbraco/ 部分。太好了,它可以与普通的 SurfaceController 一起使用,并且不需要 Route Hijacking。
    猜你喜欢
    • 2014-05-25
    • 1970-01-01
    • 2015-02-25
    • 2015-07-21
    • 2014-09-22
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    相关资源
    最近更新 更多