2017-08-09 91 views
0

我正在尋找正確配置System.Web.Handlers.TransferRequestHandlerpath屬性來處理在ASP.NET的WebAPI 2項目航線的WebAPI REST行動 ODataController 定製功能。如何爲WebApi 2和OData控制器設置IIS TransferRequestHandler路徑?

web.config文件處理程序被配置如下,以支持自定義ODataController函數調用(see my related question here):

<handlers> 
     <clear/> 
     <remove name="ExtensionlessUrlHandler-Integrated-4.0"/> 
     <remove name="OPTIONSVerbHandler"/> 
     <remove name="TRACEVerbHandler"/> 
     <add name="ExtensionlessUrlHandler-Integrated-4.0" path="/*" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/> 
    </handlers> 
    </system.webServer> 

注意,路徑設置爲/*它運作良好時訪問定製我們的OData功能ODataControllers

不過,我們也有一個ApiController,當我們訪問它,IIS不能正確處理請求和失敗,以下細節:

HTTP錯誤500.0 - 內部服務器錯誤
內部服務器錯誤

詳細錯誤信息:
模塊 ManagedPipelineHandler
通知 ExecuteRequestHandler
處理器 ExtensionlessUrlHandler集成-4.0
錯誤代碼 0x800703e9

如果我設置了TransferRequestHandlerpath*.as suggested here的的WebAPI請求得到妥善解決,不過ODataController請求最終沒有beeing與發現HTTP 400:

HTTP錯誤404.4 - 未找到
您正在查找的資源沒有與其關聯的處理程序。

詳細錯誤信息:
模塊 IIS Web核心
通知 MapRequestHandler
處理器尚未確定
錯誤代碼 0x80070002

如何正確配置它來處理這兩種情況?

++++編輯:++++
爲了清楚這裏的目的是我用來測試我的控制器查詢:

  • 定製的OData函數調用:http://localhost:xxxx/myProject/odata/SomeModels/MyNamespace.MyCustomFunction(parameterA=123,parameterB=123)
  • 本地的OData GET呼叫:http://localhost:xxxx/myProject/odata/SomeModels
  • 機Web API GET調用:http://localhost:xxxx/myProject/api/SomeOtherModel?parameterC=123

我的網頁API控制器:

public class SomeOtherModelsController : ApiController 
{ 
     public IHttpActionResult Get(int parameterC) 
     { 
      // ... 
      return Ok(/* some result */); 
     } 

     [HttpPost] 
     public IHttpActionResult Post(SomeOtherModel model) 
     { 
      // ... 
      return Ok(/* some result */); 
     } 
} 

我的OData控制器:

public class SomeModelController : ODataController 
{ 
     [EnableQuery] 
     public IHttpActionResult Get() 
     { 
      // ... 
      return Ok(/* some result*/); 
     } 

     [HttpGet] 
     [EnableQuery] 
     public IHttpActionResult MyCustomFunction(int parameterA, int parameterB) 
     { 
      // ... 
      return Ok(/* some result */); 
     } 

     [HttpGet] 
     [EnableQuery] 
     public IHttpActionResult AnotherCustomFunction() 
     { 
      // ... 
      return Ok(/* some result */); 
     } 
} 

這裏是Web API配置:

config.Routes.MapHttpRoute(
    name: "DefaultApi", 
    routeTemplate: "api/{controller}/{id}", 
    defaults: new { id = RouteParameter.Optional } 
); 

和OData的配置:

var builder = new ODataConventionModelBuilder 
{ 
    Namespace = "MyNamespace" 
}; 

builder.EntitySet<SomeModelModel>("SomeModels"); 
var anotherCustomFunction = builder.EntityType<SomeModelModel>().Collection.Function("AnotherCustomFunction"); 
anotherCustomFunction.Returns<SomeResultValue>(); 

var myCustomFunction = builder.EntityType<SomeModel>().Collection.Function("MyCustomFunction"); 
myCustomFunction.Parameter<int>("parameterA"); 
myCustomFunction.Parameter<int>("parameterB"); 
myCustomFunction.ReturnsFromEntitySet<SomeModelModel>("SomeModels"); 
+1

嗨,John-Philip,你可以發佈你的Api控制器,WebApiConfig和http請求嗎? –

+0

嗨@FrancescoBozzi,爲了清楚起見,我編輯了我的答案。不過,我已經找到了一個解決方案([見我的回答](https://stackoverflow.com/a/45588695/4306452)),而我更喜歡更具體的解決方案。 –

回答

0

一種可能的方案,as proposed here,是在web.config文件添加<modules runAllManagedModulesForAllRequests="true" /><system.webServer>

<system.webServer> 
     <modules runAllManagedModulesForAllRequests="true" /> 
</system.webServer> 

事實證明,增加這個模塊使得存在System.Web.Handlers.TransferRequestHandler處理程序不必要。

因此,以下system.webServer配置足以同時處理API查詢和定製的OData功能查詢:

<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true" /> 
    <handlers> 
     <remove name="OPTIONSVerbHandler"/> 
     <remove name="TRACEVerbHandler"/> 
    </handlers> 
    </system.webServer> 

儘管如此,我不是舒適與此解決方案,因爲我不知道exactlly可能是<modules runAllManagedModulesForAllRequests="true" />的影響。

0

我以前answer,我創建了一個新的模式(AnotherModel)和新ApiController(AnotherModelsController)

AnotherModel.cs開始

namespace DemoOdataFunction.Models 
{ 
    public class AnotherModel 
    { 
     public int Id { get; set; } 

     public int MyInt { get; set; } 

     public string MyString { get; set; } 
    } 
} 

AnotherModelsController.cs

namespace DemoOdataFunction.Controllers 
{ 
    public class AnotherModelsController : ApiController 
    { 
     public IHttpActionResult Get(int parameterC) 
     { 
      // ... 
      return Ok(); 
     } 

     public IHttpActionResult Get() 
     { 
      // ... 
      return Ok("Ok"); 
     } 

     [HttpPost] 
     public IHttpActionResult Post(AnotherModel model) 
     { 
      // ... 
      return Ok(); 
     } 
    } 
} 

Api和OData控制器均無任何其他更改。

GET 
http://localhost:4186/api/AnotherModels?parameterC=1 

GET 
http://localhost:4186/api/AnotherModels 

Post 
http://localhost:4186/api/AnotherModels 
{ 
"Id" : 1, 
"MyInt" : 2, 
"MyString" : "Hello" 
} 

GET 
http://localhost:4186/odata/TestModels/MyNamespace.MyFunction(parA=1,parB=2) 

我也試着改變web.config的「路徑」設置爲*但沒關係。 我建議你從零開始創建一個新項目並與你的項目進行比較。