如何为WebApi 2和OData控制器设置IIS TransferRequestHandler路径? [英] How to setup IIS TransferRequestHandler path for both WebApi 2 and OData Controllers?

查看:126
本文介绍了如何为WebApi 2和OData控制器设置IIS TransferRequestHandler路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望正确配置System.Web.Handlers.TransferRequestHandler path属性,以处理两者到WebApi REST操作 ODataController custom 函数的路由在ASP.NET WebApi 2项目中.

I'm looking to properly configure System.Web.Handlers.TransferRequestHandler path attribute to handle both routes to WebApi REST actions and ODataController custom function in an ASP.NET WebApi 2 project.

我的web.config文件处理程序配置如下,以支持自定义ODataController函数调用(在此处查看我的相关问题):

My web.config file handlers are configured as follow in order to support custom ODataController functions call (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>

请注意,当访问我们的ODataControllers

Note that the path is set to /* and it works well when accessing custom OData functions on our ODataControllers

尽管如此,我们也有一个ApiController,当我们访问它时,IIS无法正确处理该请求,并失败,并显示以下详细信息:

Nevertheless we also have an ApiController and when we access it, IIS doesn't properly handle the request and fails with the following details :

HTTP错误500.0-内部服务器错误
内部服务器错误

HTTP Error 500.0 - Internal Server Error
Internal Server Error

详细的错误信息:
模块 ManagedPipelineHandler
通知 ExecuteRequestHandler
处理程序 ExtensionlessUrlHandler-Integrated-4.0
错误代码 0x800703e9

Detailed Error Information:
Module ManagedPipelineHandler
Notification ExecuteRequestHandler
Handler ExtensionlessUrlHandler-Integrated-4.0
Error Code 0x800703e9

如果我将TransferRequestHandler path设置为*. 如此处建议正确解决了WebApi请求,但是ODataController请求最终没有在HTTP 400中找到蜂鸣声:

If I set the TransferRequestHandler path to *. as suggested here the WebApi request get properly resolved however the ODataController request ends up not beeing found with HTTP 400 :

HTTP错误404.4-未找到
您要查找的资源没有与之关联的处理程序.

HTTP Error 404.4 - Not Found
The resource you are looking for does not have a handler associated with it.

详细的错误信息:
模块 IIS Web核心
通知 MapRequestHandler
处理程序尚未确定
错误代码 0x80070002

Detailed Error Information:
Module IIS Web Core
Notification MapRequestHandler
Handler Not yet determined
Error Code 0x80070002

如何正确配置它以处理这两种情况?

++++++++
为了清楚起见,这里是我用来测试控制器的查询:

++++ Edit : ++++
For the sake of clarity here is the queries I use to tests my controllers :

  • 自定义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
  • Custom odata function call : http://localhost:xxxx/myProject/odata/SomeModels/MyNamespace.MyCustomFunction(parameterA=123,parameterB=123)
  • Native odata GET call : http://localhost:xxxx/myProject/odata/SomeModels
  • Native web api GET call: http://localhost:xxxx/myProject/api/SomeOtherModel?parameterC=123

我的Web 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配置:

Here is the web api configuration:

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

推荐答案

一种可能的解决方案,添加到web.config文件中的<system.webServer>:

A possible solution, as proposed here, is to add <modules runAllManagedModulesForAllRequests="true" /> to <system.webServer> in the web.config file :

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

事实证明,添加此模块使System.Web.Handlers.TransferRequestHandler处理程序的存在成为不必要.

It turns out that adding this module makes the presence of System.Web.Handlers.TransferRequestHandler handler unnecessary.

因此,以下system.webServer配置足以处理api查询和自定义OData函数查询:

Therefore the following system.webServer configuration is sufficient to handle both api query and custom OData function queries :

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

尽管如此,我对这种解决方案并不满意,因为我不太清楚<modules runAllManagedModulesForAllRequests="true" />的作用是什么.

Nevertheless, I'm not confortable with this solution as I don't know exactlly what could be the effects of <modules runAllManagedModulesForAllRequests="true" />.

这篇关于如何为WebApi 2和OData控制器设置IIS TransferRequestHandler路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆