簡體   English   中英

DNN WebAPI POST總是返回404 Not Found錯誤

[英]DNN WebAPI POST always returns a 404 Not Found Error

當我嘗試在DNN中執行服務的WebAPI方法時,GET請求沒有問題。 但是,我一生無法獲得POST請求以返回除HTTP 404 Not Found錯誤以外的任何內容。 GET和POST方法都在同一控制器中。

我檢查了類似的問題,並確認我的文件夾名稱是標准名稱,IIS沒有運行應用程序文件夾,並且我沒有其他的web.config。

我還安裝了dnnGlimpse並驗證了我的路線已注冊。

這是DNN 7.3.4,也是一個干凈的項目-它不是來自VS模板。

這是我的路線映射器的一個示例。

public class RouteMapper : IServiceRouteMapper
{
    public void RegisterRoutes(IMapRoute mapRouteManager)
    {
        mapRouteManager.MapHttpRoute("MyModule", "default", "{controller}/{action}", new[] { "MyCompany.Modules.MyModule.Controllers" });
    }
}

這是我的post方法的一個示例。 我什至無法用這種方法在任何地方都達到斷點。 此類位於名為“ Controllers”的文件夾中。

namespace MyCompany.Modules.MyModule.Controllers
{
    public class ExampleController : DnnApiController
    {
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        [HttpPost]
        public HttpResponseMessage CustomObjectType(string value)
        {
            try
            {
                var oType = CustomObjectTypeHelper.GetObjectType(value);

                switch (oType)
                {
                    case CustomObjectType.Type1:
                        return Request.CreateResponse(HttpStatusCode.OK, "type1");
                    case CustomObjectType.Type2:
                        return Request.CreateResponse(HttpStatusCode.OK, "type2");
                    case CustomObjectType.Type3:
                        return Request.CreateResponse(HttpStatusCode.OK, "type3");
                    case CustomObjectType.Type4:
                        return Request.CreateResponse(HttpStatusCode.OK, "type4");
                    default:
                        throw new ArgumentOutOfRangeException("value", "Value format does not match a supported custom object type.");
                }
            }
            catch(Exception ex)
            {
                Exceptions.LogException(ex);
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError,
                    "Card number format does not match a supported card type.");
            }
        }
    }
}

這是我如何稱呼它的一個例子。

var sf = $.ServicesFramework(<%=ModuleId %>);
var serviceUrl = sf.getServiceRoot('MyModule');

function getObjectType() {
    var strNumber = $('<%=txtNumber.ClientID %>').val();

    try
    {
        $.ajax({
            type: "POST",
            cache: false,
            url: serviceUrl + "Example/CustomObjectType",
            beforeSend: sf.setModuleHeaders,
            data: strNumber
        }).success(function(data, textStatus, jqXHR) {
            alert(data);
        }).fail(function (xhr, result, status) {
            alert("Uh-oh, something broke: " + status);
        });
    } catch (e) {
        //Shouldn't do this but it's just for testing
        alert(e.stack);
    }
}

實際上,這最終成為WebAPI不喜歡在POST中使用簡單值的問題。 一旦將其更改為預期的視圖模型,便會找到該方法。

答案在以下問題中:

找不到需要POST的簡單控制器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM