我刚刚开始编写OData服务,到目前为止我已经取得了很好的进展。我试图在我的控制器中添加两个GET方法,但问题是每当我尝试这样做时,我最终得到以下错误消息
ODataURL: http://localhost:65321/odata/Qualifications/Default.DisplayDoubleValue
{
"Message": "An error has occurred.",
"ExceptionMessage": "Multiple actions were found that match the request: \r\nGet on type WebApplication1.Controllers.QualificationsController\r\nDisplayDoubleValue on type WebApplication1.Controllers.QualificationsController",
"ExceptionType": "System.InvalidOperationException",
"StackTrace": " at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)\r\n at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)\r\n at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()"
}
我查看了一些StackOverflow帖子并尝试遵循相同的但我能够调用自定义方法的唯一方法是,如果我的控制器中只有一个HTTPGet attribute
// GET: odata/Qualifications
[HttpGet]
[EnableQuery]
public IQueryable<Qualification> Get()
{
return db.Qualifications;
}
[HttpGet]
public double DisplayDoubleValue()
{
return 12.0;
}
[HttpGet]
public string HelloName([FromODataUri] string name)
{
return "Hello" + name;
}
public static void Register(HttpConfiguration config)
{
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Qualification>("Qualifications");
//custom function configure 1
var customFunctionGetHelloName = builder.EntityType<Qualification>().Collection.Function("HelloName");
customFunctionGetHelloName.Parameter<string>("name");
customFunctionGetHelloName.Returns<string>();
//custom function configure 2
var customFunctionGetDisplayDoubleVal = builder.EntityType<Qualification>().Collection.Function("DisplayDoubleValue");
customFunctionGetDisplayDoubleVal.Returns<double>();
config.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
config.Routes.MapHttpRoute( //MapHTTPRoute for controllers inheriting ApiController
name: "DefaultApiByAction",
routeTemplate: "api/{controller}/{action}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute( //MapHTTPRoute for controllers inheriting ApiController
name: "DefaultApiByActionId",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute( //MapHTTPRoute for controllers inheriting ApiController
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
正如其他一些帖子中所提到的,我在web.config文件中添加了以下内容以避免IIS路由问题,但这样做也无济于事
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules >
<remove name="ApplicationInsightsWebTracking" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
</modules>
<handlers>
<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元数据向我显示它有两个我可以调用的动作方法,但我想我在路由中的某处弄得一团糟,我无法弄清楚
<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Default">
<Function Name="HelloName" IsBound="true">
<Parameter Name="bindingParameter" Type="Collection(WebApplication1.Models.Qualification)"/>
<Parameter Name="name" Type="Edm.String" Unicode="false"/>
<ReturnType Type="Edm.String" Unicode="false"/>
</Function>
<Function Name="DisplayDoubleValue" IsBound="true">
<Parameter Name="bindingParameter" Type="Collection(WebApplication1.Models.Qualification)"/>
<ReturnType Type="Edm.Double" Nullable="false"/>
</Function>
请让我知道我弄得一团糟的地方是我需要在控制器中添加的路由或某些属性才能使其正常工作。