我在项目中使用了Umbraco 7.3。我创建了一个自定义数据类型,但当我想在Surfacecontroller
或HelloSurfaceController
中调用Hello2SurfaceController
时,我在umbraco后台出现错误Request error: The URL returned a 404 (not found):
我研究了一些关于路由的文章,但我无法解决我的问题。我不知道我做错了什么。
我该如何解决这个问题?
Reply.controller.js:
angular.module("umbraco")
.controller("Reply.controller", function ($scope, $http) {
$scope.SendReply = function () {
var sendTo = $("#Email").val();
var textMessage = $("#TextMessage").val();
$scope.xxx = "I'm here!";
var data = { SendTo: sendTo, TextMessage: textMessage };
// ~/Hello2Surface/ReplyMessage ---> Cannot find this URL
$http.post("~/App_Plugins/Reply/HelloSurface/ReplyMessage") // Can not find this URL
.then(function (response) {
alert("YES!");
//TODO:
});
}
});
SurfaceController
namespace Jahan.Nuts.Web.Mvc.UmbracoCms.App.App_Plugins.Reply
{
public class HelloSurfaceController : SurfaceController
{
[HttpPost][ChildActionOnly]
public ActionResult ReplyMessage()
{
//TODO: how should be write this method that be proper for getting data from angularjs?
return null;
}
}
}
package.manifest
{
propertyEditors: [
{
alias: "Send.Reply",
name: "Send Reply",
editor:{
view:"~/App_Plugins/Reply/Reply.html"
},
}
]
,
javascript:[
'~/App_Plugins/Reply/Reply.controller.js'
]
}
Reply.html
<div ng-controller="Reply.controller">
<div style="width: 100%;">
<input type="button" value="Send Reply" title="SendReply" name="Send Reply" ng-click="SendReply()" />
</div>
<div>
<input type="text" ng-model="xxx" name="message" />
</div>
答案 0 :(得分:3)
仔细查看文档 - 特别是基于插件的SurfaceControllers
部分:
https://our.umbraco.org/documentation/Reference/Routing/surface-controllers
尝试这样做(请注意PluginController
属性):
namespace Jahan.Nuts.Web.Mvc.UmbracoCms.App.App_Plugins.Reply
{
[PluginController("Reply")]
public class HelloSurfaceController : SurfaceController
{
[HttpPost][ChildActionOnly]
public ActionResult ReplyMessage()
{
//TODO: how should be write this method that be proper for getting data from angularjs?
return null;
}
}
}
其他注释:
UmbracoApiController
。查看https://our.umbraco.org/documentation/Reference/Routing/WebApi/了解更多信息(包括关于Api端点的预期位置的说明)编辑已添加&#34;更正&#34;这样做的方法:
如上所述,要实现UmbracoApiController,您需要一个类似于此的类 - 请注意,如果您不需要担心授权,可以使用UmbracoApiController:
namespace Jahan.Nuts.Web.Mvc.UmbracoCms.App.App_Plugins.Reply
{
[PluginController("Reply")]
public class HelloApiController : UmbracoAuthorizedApiController
{
public void PostReplyMessage(string to, string message)
{
// TODO: process your message and then return something (if you want to).
}
}
}
然后在AngularJS中设置这样的资源:
function replyResource($q, $http, umbDataFormatter, umbRequestHelper) {
var replyResource = {
sendMessage: function (sendTo, msg) {
return umbRequestHelper.resourcePromise(
$http.post("Backoffice/Reply/HelloApi/PostReplyMessage?" +
umbRequestHelper.dictionaryToQueryString(
[{ to: sendTo }, { message: msg }])),
'Failed to send message to ' + sendTo + ': ' + msg);
}
};
return replyResource;
}
angular.module('umbraco.resources').factory('replyResource', replyResource);
最后你的实际视图控制器可以使用如下:
angular.module("umbraco")
.controller("Reply.controller", function ($scope, $http, $injector) {
// Get a reference to our resource - this is why we need the $injector specified above
replyResource = $injector.get('replyResource');
$scope.SendReply = function () {
// You really shouldn't use jQuery here - learn to use AngularJS Bindings instead and bind your model properly.
var sendTo = $("#Email").val();
var textMessage = $("#TextMessage").val();
replyResource.sendMessage(sendTo, textMessage)
.then(function (response) {
// Success
}, function (err) {
// Failure
});
}
};
});
那里可能存在一些错误;我主要是从记忆中做到这一点 - 特别是,您可能需要研究将数据发布到ApiController的最佳方式 - 它不太可能只接受这样的两个参数。
有关更完整的示例,请考虑查看Umbraco MemberListView插件的代码:https://github.com/robertjf/umbMemberListView
另外,您真的应该阅读上面列出的ASP.Net MVC基础知识和面向SurfaceControllers和APIControllers的Umbraco文档。
答案 1 :(得分:1)
从网址中删除“Surface”并包含“后台”:
angular.module("umbraco")
.controller("Reply.controller", function ($scope, $http) {
$scope.SendReply = function () {
var sendTo = $("#Email").val();
var textMessage = $("#TextMessage").val();
$scope.xxx = "I'm here!";
var data = { SendTo: sendTo, TextMessage: textMessage };
// ~/Hello2Surface/ReplyMessage ---> Cannot find this URL
$http.post("backoffice/Reply/Hello/ReplyMessage") // Can not find this URL
.then(function (response) {
alert("YES!");
//TODO:
});
}
});
另外,我建议使用UmbracoAuthorizedController而不是表面控制器,因为登录的用户会在后端使用它,因此保证它的安全是明智的。
所以你的控制器看起来应该是这样的:
[PluginController("Reply")]
namespace Jahan.Nuts.Web.Mvc.UmbracoCms.App.App_Plugins.Reply
{
public class HelloApiController : UmbracoAuthorizedJsonController
{
public [Model-to-be-returned-to-angular] ReplyMessage()
{
//sql query etc to populate model
//return model
}
}
}