【发布时间】: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