Reputation: 1680
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);
}
}
Upvotes: 0
Views: 1462
Reputation: 1680
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
Upvotes: 2