繁体   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