114

私が機能させようとしている URL は、http ://somedomain.com/api/people/staff.33311 のスタイルのものです(LAST.FM のようなサイトが RESTFul および WebPage の URL であらゆる種類の記号を許可するように)たとえば、" http://www.last.fm/artist/psy'aviah " は LAST.FM の有効な URL です)。

動作するのは次のシナリオです: - http://somedomain.com/api/people/ - すべての人を返します - http://somedomain.com/api/people/staff33311 - 同様に動作しますが、それは私のものではありません以下の例のように、「ドット」を受け入れる URL が必要な場合は、次のように m します - http://somedomain.com/api/people/staff.33311 - しかし、これは私に

HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

私は次のことを設定しました:

  1. コントローラー「PeopleController」

    public IEnumerable<Person> GetAllPeople()
    {
        return _people;
    }
    
    public IHttpActionResult GetPerson(string id)
    {
        var person = _people.FirstOrDefault(p => p.Id.ToLower().Equals(id.ToLower()));
        if (person == null)
            return NotFound();
    
        return Ok(person);
    }    
    
  2. WebApiConfig.cs

    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
    
        // Web API routes
        config.MapHttpAttributeRoutes();
    
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
    

私はすでにこのブログ投稿http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspxのすべてのヒントに従ってみましたが、それでもうまくいきません。より良く、より安全な方法。

内部的にはこのように Id を持っているので、できれば "." のスタイルで、何らかの方法でドットに適合する解決策を見つける必要があります。しかし、必要に応じて、URL の代替提案を受け入れます...

4

9 に答える 9

146

http://somedomain.com/api/people/staff.33311/の代わりにスラッシュなどを URL の末尾に付けますhttp://somedomain.com/api/people/staff.33311

于 2015-07-28T12:57:46.920 に答える
115

web.configファイルの次の設定により、問題が解決するはずです。

<configuration>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
于 2014-01-08T15:47:34.397 に答える
36

標準のに次を追加すると、問題が解決することがわかりました。ExtensionlessUrlHandler

<add name="ExtensionlessUrlHandler-Integrated-4.0-ForApi"
     path="api/*"
     verb="*"
     type="System.Web.Handlers.TransferRequestHandler"
     preCondition="integratedMode,runtimeVersionv4.0" />

IDE(私の場合はVisual Studio)がサイト構成を管理している場合に役立つことを除いて、名前は実際にはそれほど重要ではないと思います.

https://stackoverflow.com/a/15802305/264628への H/T

于 2016-06-01T21:10:52.620 に答える
25

私は実際に何をしているのかわかりませんが、前の回答を少し試した後、おそらくより適切な別の解決策を思いつきました:

<system.webServer>
<modules>
    <remove name="UrlRoutingModule-4.0" />
    <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" />
</modules>
</system.webServer>
于 2014-03-17T20:49:32.697 に答える
11

runAllManagedModulesForAllRequests属性を に設定するだけでなく、それ以上のことを行う必要があることがわかりましたtrue。また、拡張子のない URL ハンドラーがすべてのパスを参照するように構成されていることも確認する必要がありました。さらに、追加できるもう 1 つのボーナス構成設定があり、場合によっては役立ちます。これが私の作業中のWeb.configです:

<system.web>
    <httpRuntime relaxedUrlToFileSystemMapping="true" />
</system.web>
<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers>
        <remove name="WebDAV" />
        <remove name="OPTIONSVerbHandler" />
        <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
        <add name="ExtensionlessUrlHandler-Integrated-4.0"  path="*" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
</system.webServer>

ExtensionlessUrlHandler-Integrated-4.0具体的には、のpath属性が(たとえば) では*なく に設定されていることに注意してください。*.

于 2016-05-06T19:58:37.413 に答える