2017-08-09 90 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的“路径”设置为*但没关系。 我建议你从零开始创建一个新项目并与你的项目进行比较。