控制器UmbracoApiController上未找到任何操作

时间:2016-06-28 12:18:37

标签: c# json ajax umbraco7

我要做的是从Umbraco Api类访问数据并将其传递给javascript函数。所有帮助都归功于克服以下错误。 -

{,…}
Message
:
"No HTTP resource was found that matches the request URI 'http://mywebsite.local/Umbraco/Api/MapApi/GetMapMarkers/4188'."
MessageDetail
:
"No action was found on the controller 'MapApi' that matches the request."

UmbracoApiController处理应传递给调用AJAX的Google Map数据和Umbraco字段数据 -

public class MapApiController :  UmbracoApiController
    {

        [HttpPost]
        public void GetMapMarkers(int nodeId)
        {
            var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
            var contentItem = new Node(nodeId);

            // Get the map values
            var map = contentItem.GetProperty("mapLocation").Value;
            var lat = map.Split(',')[0];
            var lng = map.Split(',')[1];
            var zoom = map.Split(',')[2];

            var infoTitle = string.Empty;
            if (contentItem.GetProperty("mapLocationTitle") != null)
            {
                infoTitle = contentItem.GetProperty("mapLocationTitle").Value;
            }

            var infoText = string.Empty;
            if (contentItem.GetProperty("mapLocationInfo") != null)
            {
                infoText = contentItem.GetProperty("mapLocationInfo").Value;
            }

            string markerImageId;
            IPublishedContent markerImage;

            var mapSettings = JJWHelper.GetMapSettings();
            var markerImageUrl = mapSettings.MapPointerImage;

            if (contentItem.HasProperty("mapPointerImage"))
            {
                markerImageId = contentItem.GetProperty("mapPointerImage").Value;
                markerImage = umbracoHelper.TypedMedia(markerImageId);
                markerImageUrl = markerImage.Url;
            }

            var markers = new Marker[contentItem.ChildrenAsList.Count + 1];

            markers[0] = new Marker
            {
                MapLocation = map,
                Lat = Convert.ToDouble(lat),
                Lng = Convert.ToDouble(lng),
                Zoom = Convert.ToDouble(zoom),
                InfoTitle = !string.IsNullOrEmpty(infoTitle) ? infoTitle : " ",
                InfoText = !string.IsNullOrEmpty(infoText) ? infoText : " ",
                MarkerImageUrl = markerImageUrl
            };

            var loopCtr = 1;

            foreach (var pointer in contentItem.ChildrenAsList)
            {
                map = pointer.GetProperty("mapLocation").Value;
                lat = map.Split(',')[0];
                lng = map.Split(',')[1];
                infoTitle = pointer.GetProperty("mapLocationTitle") != null ? pointer.GetProperty("mapLocationTitle").Value : string.Empty;
                infoText = pointer.GetProperty("mapLocationInfo") != null ? pointer.GetProperty("mapLocationInfo").Value : string.Empty;
                markerImageId = pointer.GetProperty("mapPointerImage").Value;
                markerImage = umbracoHelper.TypedMedia(markerImageId);

                // Marker Image is mandatory, but issue (?) in Umbraco is allowing pointer node
                // to be published even though a pointer image has not been selected.
                // So, check that we actually have a marker image before adding it to the output array
                if (markerImage == null)
                {
                    continue;
                }

                markerImageUrl = markerImage.Url;
                markers[loopCtr] = new Marker
                {
                    MapLocation = map,
                    Lat = Convert.ToDouble(lat),
                    Lng = Convert.ToDouble(lng),
                    Zoom = Convert.ToDouble(zoom),
                    InfoTitle = !string.IsNullOrEmpty(infoTitle) ? infoTitle : " ",
                    InfoText = !string.IsNullOrEmpty(infoText) ? infoText : " ",
                    MarkerImageUrl = markerImageUrl
                };

                loopCtr++;
            }

            var json = new JavaScriptSerializer().Serialize(markers);

            HttpContext.Current.Response.ContentType = "application/json";
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
            HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddDays(10));
            HttpContext.Current.Response.Cache.SetETag(nodeId.ToString(CultureInfo.InvariantCulture));
            HttpContext.Current.Response.Write(json);
        }

AJAX电话 -

  var markers = (function () { 
            var json = null; 
            $.ajax({
                'async': false, 
                'global': false, 
                'url': "/Umbraco/Api/MapApi/GetMapMarkers/" + nodeId, 
                'dataType': "json",
                 'type': "POST",
                'success': function (data) {
                    json = data; } }); 
            return json;}

3 个答案:

答案 0 :(得分:1)

这应该有效

$.ajax({
    'async': false, 
    'global': false, 
    'url': "/Umbraco/Api/MapApi/GetMapMarkers/", 
    'dataType': "json",
    'data': { nodeId: nodeId},
    'type': "POST",
    'success': function (data) {
        json = data; } 
}); 

在你的c#代码中:

public class TestObj
{
    public int nodeId { get; set; }
}

public class MapApiController : UmbracoApiController
{

    [System.Web.Http.HttpPost]
    public void GetMapMarkers(TestObj t)
    {
        // ..
    }
}

答案 1 :(得分:1)

确保使用正确的`HttpPost'属性。

标准它将使用System.Web.Mvc.HttpPost属性,您需要System.Web.Http.HttpPost属性。

答案 2 :(得分:0)

最后问题是本地服务器设置被设置为欧洲设置,因此点被替换为逗号!现在全部修复,非常感谢所有发布的人!