2015-04-03 114 views
0

當我嘗試在DNN中執行服務的WebAPI方法時,GET請求沒有問題。然而,我不能爲我的生活得到一個POST請求來返回除HTTP 404 Not Found錯誤之外的任何內容。 GET和POST方法都在同一個控制器中。DNN WebAPI POST始終返回404 Not Found Error

我回顧了類似的問題,並確認我的文件夾名稱是標準的,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方法的一個例子。在這種方法中,我甚至無法在任何地方打斷點。該課程位於名爲「控制器」的文件夾中。

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

回答

相關問題