c# - OpenRasta 是否支持 HEAD HTTP 方法?

标签 c# rest openrasta

我正在尝试配置 OpenRasta 资源以响应 HTTP HEAD 操作。我已经尝试使用 UriName、不同的方法名称和签名,但我还找不到解决方案。这是我使用的配置代码:

ResourceSpace.Has.ResourcesOfType<Stream>()
                 .AtUri("/assets/{providerCode}/?type={type}")
                 .HandledBy<AssetHandler>();

这是 AssetHandler 中的方法:

[HttpOperation(HttpMethod.HEAD)]
public OperationResult Head(string providerCode, int type)
{
    var asset = assetService.GetAssetAtCDN(providerCode, type);
    if (asset == null) return new OperationResult.NotFound();

    return new OperationResult.Found { RedirectLocation = new Uri(asset.Url) };
}

示例请求:

HEAD http://localhost:63483/assets/PygvCaOfK353etajBlbd/?type=4 HTTP/1.1
Accept-Encoding: gzip,deflate
Host: localhost:63483

响应:

HTTP/1.1 405 Method Not Allowed
Server: ASP.NET Development Server/9.0.0.0
Date: Mon, 31 Oct 2011 17:40:39 GMT
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Length: 0
Connection: Close

在 OpenRasta 日志中:

DEBUG Entering OpenRastaRewriterHandler: Rewriting to original path e:
DEBUG Entering OpenRastaIntegratedHandler: Request for http://localhost:63483/assets/PygvCaOfK353etajBlbd/?type=4 e:
DEBUG Found 0 operation(s) with a matching name. e:
DEBUG Found 0 operation(s) with matching [HttpOperation] attribute. e:
INFO Executing OperationResult OperationResult: type=MethodNotAllowed, statusCode=405. e:
INFO No response codec was searched for. The response entity is null or a response codec is already set. e:
DEBUG There was no response entity, not rendering. e:
DEBUG Writing http headers. e:
DEBUG Exiting OpenRastaIntegratedHandler e:
DEBUG Exiting OpenRastaRewriterHandler e:

这是 web.config 的干净版本:

  <system.web>
    <pages>
      <namespaces>
        <add namespace="OpenRasta.Web"/>
        <add namespace="OpenRasta.Web.Markup"/>
        <add namespace="OpenRasta"/>
      </namespaces>
      <controls>
        <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </controls>
    </pages>
    <httpHandlers>
      <remove verb="*" path="*.asmx"/>
      <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
      <!--OpenRasta-->
      <add verb="*" path="*.rastahook" type="OpenRasta.Hosting.AspNet.OpenRastaHandler, OpenRasta.Hosting.AspNet"/>
    </httpHandlers>
    <httpModules>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <!--OpenRasta-->
      <add name="OpenRastaModule" type="OpenRasta.Hosting.AspNet.OpenRastaModule, OpenRasta.Hosting.AspNet"/>
    </httpModules>
  </system.web>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules>
      <remove name="ScriptModule"/>
      <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add name="OpenRastaModule" type="OpenRasta.Hosting.AspNet.OpenRastaModule, OpenRasta.Hosting.AspNet"/>
    </modules>
    <handlers>
      <remove name="WebServiceHandlerFactory-Integrated"/>
      <remove name="ScriptHandlerFactory"/>
      <remove name="ScriptHandlerFactoryAppServices"/>
      <remove name="ScriptResource"/>
      <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add name="OpenRastaHandler" verb="*" path="*.rastahook" type="OpenRasta.Hosting.AspNet.OpenRastaHandler, OpenRasta.Hosting.AspNet"/>
    </handlers>
  </system.webServer>

最佳答案

您可以为此使用 ResourcesNamed:

ResourceSpace.Has.ResourcesNamed("head")
             .AtUri("/assets/{providerCode}/?type={type}")
             .HandledBy<AssetHandler>();

关于c# - OpenRasta 是否支持 HEAD HTTP 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7957969/

相关文章:

c# - dirinfor.EnumerateFiles -> 错误

azure - 如何使用 Azure 数据工厂管道调用 REST API?

rest - 从 go 代码调用用 java 编写的 rest API

c# - 根据条件在 GridView 中显示图像

c# - .NET 中的 HttpWebRequest NameResolutionFailure 异常(在 Ubuntu 上使用 Mono)

c# - 聚合 C# 数据集时,如何将 DataSet 转换为 .Load() 的 DataTable 数组?

api - 具有经过身份验证的用户的 REST api 的无状态性

OpenRasta DI PerRequest 生命周期问题

javascript - 如何在 OpenRasta 中返回静态 HTML 和/或 Javascript

rest - 如何在 REST 中支持部分更新 (PATCH)