簡體   English   中英

OData v4函數始終返回404

[英]OData v4 Function always returns 404

試圖從OData v3遷移到OData v4 為什么我在嘗試使用OData函數時會繼續獲得404

Web API配置:

ODataModelBuilder builder = new ODataConventionModelBuilder();
//etc
builder.EntitySet<LocalizableString>("LocalizableStringApi");
//etc
var getComparitiveTableFunction = builder.EntityType<LocalizableString>().Collection.Function("GetComparitiveTable");
getComparitiveTableFunction.Parameter<string>("cultureCode");
getComparitiveTableFunction.ReturnsCollection<ComparitiveLocalizableString>();
//etc
config.MapODataServiceRoute("OData_Kore_CMS", "odata/kore/cms", builder.GetEdmModel());

C#代碼:

[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
[HttpGet]
//[ODataRoute("Default.GetComparitiveTable(cultureCode={cultureCode})")] // Tried this, but gets errors and I noticed the function is in the OData model anyway without this, so should be fine.
public virtual IHttpActionResult GetComparitiveTable([FromODataUri] string cultureCode)
{
    // Implementation
    return Ok(query);
}

從$ metadata返回的XML:

<Schema Namespace="Default">
    <Function Name="GetComparitiveTable" IsBound="true">
        <Parameter Name="bindingParameter" Type="Collection(Kore.Localization.Domain.LocalizableString)"/>
        <Parameter Name="cultureCode" Type="Edm.String" Unicode="false"/>
        <ReturnType Type="Collection(Kore.Localization.Models.ComparitiveLocalizableString)"/>
    </Function>
    ...

如您所見,它位於架構/ OData模型中......但以下查詢不起作用:

http://localhost:30863/odata/kore/cms/LocalizableStringApi/Default.GetComparitiveTable(cultureCode='en-US')

我也嘗試過以下方法:

http://localhost:30863/odata/kore/cms/LocalizableStringApi/GetComparitiveTable(cultureCode='en-US')
http://localhost:30863/odata/kore/cms/Default.GetComparitiveTable(cultureCode='en-US')
http://localhost:30863/odata/kore/cms/GetComparitiveTable(cultureCode='en-US')

所有上述結果都是404

那么......我在這里做錯了什么?

我解決了向請求的URL添加尾部斜杠的類似問題。

我通過在web.config中的 <system.webServer>下添加以下行來解決這個問題:

<modules runAllManagedModulesForAllRequests="true">

如果我沒記錯的話,這可能會導致性能問題。 所以這不太理想。 任何更好的解決方案都非常受歡

您需要一個名為UrlRoutingModule-4.0的模塊才能在IIS中運行。 此解決方案會導致所有已注冊的HTTP模塊在每個請求上運行,而不僅僅是托管請求(例如.aspx)。 這意味着模塊將運行在.jpg .gif .css .html .pdf等上。

因此,更好的解決方案是在web.config中添加以下內容

<modules>
  <remove name="UrlRoutingModule-4.0" />
  <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
</modules>

資料來源: http//www.britishdeveloper.co.uk/2010/06/dont-use-modules-runallmanagedmodulesfo.html

這是一個使用OData函數/操作來防止404 Not Found錯誤的解決方案。

此解決方案的好處

web.config中添加這些行

<system.webServer>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0Custom" path="/odata*" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>

EtVoilà:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM