C# 如何为WebApi 2和OData控制器设置IIS TransferRequestHandler路径?

C# 如何为WebApi 2和OData控制器设置IIS TransferRequestHandler路径?,c#,asp.net,asp.net-web-api,asp.net-web-api2,C#,Asp.net,Asp.net Web Api,Asp.net Web Api2,我希望在ASP.NET WebApi 2项目中正确配置System.Web.Handlers.TransferRequestHandlerpath属性,以处理到WebApi REST操作的路由和ODataController自定义函数 Myweb.config文件处理程序配置如下,以支持自定义ODataController函数调用(): 我的odata控制器: public class SomeOtherModelsController : ApiController { pu

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

My
web.config
文件处理程序配置如下,以支持自定义ODataController函数调用():

我的odata控制器:

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

        [HttpPost]
        public IHttpActionResult Post(SomeOtherModel model)
        {
            // ...
            return Ok(/* some result */);
        }
}
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");
var builder=新的ODataConventionModelBuilder
{
Namespace=“MyNamespace”
};
builder.EntitySet(“某些模型”);
var anotherCustomFunction=builder.EntityType().Collection.Function(“anotherCustomFunction”);
另一个CustomFunction.Returns();
var myCustomFunction=builder.EntityType().Collection.Function(“myCustomFunction”);
参数(“参数”);
参数(“参数B”);
myCustomFunction.ReturnsFromEntitySet(“SomeModels”);
一个可能的解决方案是将
添加到
web.config
文件中的

 <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
 </system.webServer>
尽管如此,我不能接受这个解决方案,因为我不确切知道

从我以前的工作开始,我创建了一个新模型(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的“path”设置更改为*但没关系。
我建议您从头开始创建一个新项目,并将其与您的项目进行比较。

嗨,John Philip,您能发布您的Api控制器、WebApiConfig和http请求吗?嗨@FrancescoBozzi,为了清晰起见,我编辑了我的答案。尽管如此,我已经找到了一个解决方案(),而我更喜欢一个更具体的解决方案。
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();
        }
    }
}
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)