【问题标题】:DNN WebAPI POST always returns a 404 Not Found ErrorDNN WebAPI POST 总是返回 404 Not Found 错误
【发布时间】:2015-06-09 01:05:01
【问题描述】:

当我尝试在 DNN 中做服务的 WebAPI 方法时,我对 GET 请求没有任何问题。但是,除了 HTTP 404 Not Found 错误之外,我无法得到一个 POST 请求来返回任何内容。 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);
    }
}

【问题讨论】:

    标签: c# asp.net ajax dotnetnuke asp.net-web-api


    【解决方案1】:

    实际上,这最终成为 WebAPI 不喜欢在 POST 中使用简单值的问题。一旦我将其更改为期望视图模型,就会找到该方法。

    答案在下面的问题中:

    Simple controller which takes POST is not found

    【讨论】:

      猜你喜欢
      • 2012-08-16
      • 2016-11-19
      • 1970-01-01
      • 2018-01-04
      • 2021-03-31
      • 2013-08-13
      • 1970-01-01
      • 2021-10-14
      • 2019-12-09
      相关资源
      最近更新 更多