简体   繁体   English

DNN WebAPI POST总是返回404 Not Found错误

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

I have no problem with GET requests when I try the WebAPI method of doing services in DNN. 当我尝试在DNN中执行服务的WebAPI方法时,GET请求没有问题。 However, I can't for the life of me get a POST request to return anything but a HTTP 404 Not Found error. 但是,我一生无法获得POST请求以返回除HTTP 404 Not Found错误以外的任何内容。 Both the GET and POST methods are in the same controller. GET和POST方法都在同一控制器中。

I reviewed similar questions and confirmed that my folder names are standard, IIS is not running application folders, and I don't have an additional web.config. 我检查了类似的问题,并确认我的文件夹名称是标准名称,IIS没有运行应用程序文件夹,并且我没有其他的web.config。

I also installed dnnGlimpse and verified that my route was registered. 我还安装了dnnGlimpse并验证了我的路线已注册。

This is DNN 7.3.4 and a clean project - it's not from a VS template. 这是DNN 7.3.4,也是一个干净的项目-它不是来自VS模板。

This is an example of my route mapper. 这是我的路线映射器的一个示例。

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

This is an example of my post method. 这是我的post方法的一个示例。 I can't even hit a breakpoint anywhere in this method. 我什至无法用这种方法在任何地方都达到断点。 This class is in a folder named "Controllers." 此类位于名为“ 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.");
            }
        }
    }
}

Here is an example of how I have called it. 这是我如何称呼它的一个例子。

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);
    }
}

Actually, this ended up being the issue where WebAPI doesn't like to use simple values with POST. 实际上,这最终成为WebAPI不喜欢在POST中使用简单值的问题。 Once I changed this to expect a view model, the method is found. 一旦将其更改为预期的视图模型,便会找到该方法。

The answer is in the question below: 答案在以下问题中:

Simple controller which takes POST is not found 找不到需要POST的简单控制器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM