我正在将我现有的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表面控制器的文章,可以说表面控制器仅适用于" normal"表格帖子和返回,即" CurrentUmbracoPage"。
然后是UmbracoApiController,但由于ApiController是无状态的,因此不可能使用涉及会话或cookie的逻辑。
如果有人提出解决方案或最佳做法,我将感激不尽。
祝你好运
答案 0 :(得分:0)
我猜你使用错误的URL来调用动作,因为使用surfacecontroller是好的,你应该没有任何问题使用它。在Umbraco,如果你想调用控制器的动作,你应该使用这种格式的Url:
a["Bush George H. W."]="George H. W. Bush"
您还可以查看以下链接:https://our.umbraco.org/documentation/reference/routing/surface-controllers
答案 1 :(得分:0)
在对SurfaceControllers进行异步调用时,不能使用UmbracoContext,但是,您的上述代码确实有效。你确定你的网址正确吗?
不会从路线中移除Surface部分,因此您的网址应为: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);
}