简体   繁体   中英

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. However, I can't for the life of me get a POST request to return anything but a HTTP 404 Not Found error. Both the GET and POST methods are in the same controller.

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.

I also installed dnnGlimpse and verified that my route was registered.

This is DNN 7.3.4 and a clean project - it's not from a VS template.

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. I can't even hit a breakpoint anywhere in this method. This class is in a folder named "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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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