1
我試圖讓我的WebAPI OData 4控制器處理OPTIONS動詞。ODataController和OPTIONS動詞
我有下面的代碼在我的控制器:
[AcceptVerbs("OPTIONS")]
public IHttpActionResult Options()
{
return Ok();
}
但我仍然在OPTIONS請求獲得404試圖做使用jQuery文章時。
我試圖讓我的WebAPI OData 4控制器處理OPTIONS動詞。ODataController和OPTIONS動詞
我有下面的代碼在我的控制器:
[AcceptVerbs("OPTIONS")]
public IHttpActionResult Options()
{
return Ok();
}
但我仍然在OPTIONS請求獲得404試圖做使用jQuery文章時。
似乎當前的默認OData路由約定不知道如何處理OPTIONS謂詞。您可以創建自定義的IODataRoutingConvention
來處理該動詞。
public class OptionsRoutingConvention : IODataRoutingConvention
{
public string SelectController(ODataPath odataPath, HttpRequestMessage request)
{
if (request.Method == HttpMethod.Options)
{
// ...
return "ControllerName";
}
return null;
}
public string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup<string, HttpActionDescriptor> actionMap)
{
if (controllerContext.Request.Method == HttpMethod.Options)
{
// ...
return "Action";
}
return null;
}
,然後添加到您的配置:
IEnumerable<IODataRoutingConvention> conventions =
ODataRoutingConventions.CreateDefault().Concat(new[] {new OptionsRoutingConvention()});
config.MapODataServiceRoute("odata", "v2", builder.GetEdmModel(), new DefaultODataPathHandler(), conventions);