gpt4 book ai didi

c# - 在 Controller UmbracoApiController 上未找到任何操作

转载 作者:行者123 更新时间:2023-11-30 23:23:53 33 4
gpt4 key购买 nike

我正在尝试做的是从 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;}

最佳答案

这应该可行

$.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)
{
// ..
}
}

关于c# - 在 Controller UmbracoApiController 上未找到任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38076075/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com