点字符'.' 在MVC Web API 2中请求诸如api/people/STAFF.45287之类的请求

Yve*_*lpe 102 c# asp.net-mvc-routing asp.net-mvc-4 asp.net-web-api asp.net-web-api2

我试图让工作的URL是以下风格的网址:http://somedomain.com/api/people/staff.33311(就像网站一样,LAST.FM允许在他们的RESTFul和WebPage网址中添加所有类型的标记例如," http://www.last.fm/artist/psy'aviah "是LAST.FM的有效网址.

以下方案有效: - http://somedomain.com/api/people/ - 返回所有人 - http://somedomain.com/api/people/staff33311 - 也可以,但不是我的意思在我希望网址接受"点"后,就像下面的示例 - 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.
Run Code Online (Sandbox Code Playgroud)

我已经设置了以下内容:

  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);
    }    
    
    Run Code Online (Sandbox Code Playgroud)
  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 }
        );
    }
    
    Run Code Online (Sandbox Code Playgroud)

我已经尝试过这篇博文的所有提示http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx但它仍然无法工作..我也认为这很乏味,我想知道是否没有其他的,更好,更安全的方式.

我们内部有这样的Id,因此我们必须找到一种解决方案,以这种或那种方式适合点,最好是"."的风格.但如果需要,我愿意接受网址的其他建议......

Dan*_*rod 136

使用斜杠http://somedomain.com/api/people/staff.33311/替换URL,例如而不是http://somedomain.com/api/people/staff.33311.

  • 但是当点位于URL部分的末尾时,这不起作用,如/ people/member. (5认同)
  • @AgustinMeriles可以将我的答案视为一种替代方法,而不是实际答案,这取决于您对问题的解释。 (3认同)
  • 不,如果我最后不想要斜线怎么办?!它不应该被要求. (2认同)

Kir*_*lla 98

在您的web.config文件中进行以下设置后,应解决以下问

<configuration>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
Run Code Online (Sandbox Code Playgroud)

  • http://stackoverflow.com/a/12151501/167018值得关注一下,如果您担心(并且理所当然地)启用runAllManagedModulesForAllRequests对性能的影响. (9认同)
  • 它成功了.但是,通过设置此选项是否存在任何漏洞?为什么这不是标准行为? (4认同)
  • 当我尝试这个时,只有在路径的末尾放一个'/'才能工作.有什么建议为什么会这样?在旁注中,下面的答案专门添加了'UrlRoutingModule'而不是运行所有模块,也为我工作,尽管仍然存在需要'/'才能工作的问题. (3认同)
  • 好的,为了任何人的兴趣:我在上面的回答中看到我上面的问题在接受答案时回答(Kapil Khandelwal回答):http://stackoverflow.com/questions/11048863/modules-runallmanagedmodulesforallrequests-true-meaning (2认同)
  • 是的,它工作,总线其他人,我想知道这个功能需要哪个特定的模块工作? (2认同)

Bri*_*anS 33

我发现在标准之前添加以下内容可以解决我的问题:ExtensionlessUrlHandler

<add name="ExtensionlessUrlHandler-Integrated-4.0-ForApi"
     path="api/*"
     verb="*"
     type="System.Web.Handlers.TransferRequestHandler"
     preCondition="integratedMode,runtimeVersionv4.0" />
Run Code Online (Sandbox Code Playgroud)

我认为名称实际上并不重要,除非你的IDE(在我的情况下是Visual Studio)管理你的站点配置可能会有所帮助.

H/T到/sf/answers/1106161381/

  • 感谢BEFORE,因为它没有工作! (2认同)

Gre*_* Z. 24

我不知道我到底在做什么,但在使用之前的答案后,我想出了另一个,也许更合适的解决方案:

<system.webServer>
<modules>
    <remove name="UrlRoutingModule-4.0" />
    <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" />
</modules>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

  • 这不是重定位到列表末尾,而是删除了此模块仅针对托管处理程序运行的默认前提条件。默认配置使用这种格式:`&lt;add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="managedHandler" /&gt;` (2认同)

Jos*_* M. 8

我发现我需要做的不仅仅是设置runAllManagedModulesForAllRequests属性true.我还必须确保将无扩展名URL处理程序配置为查看所有路径.此外,您还可以添加一个额外的奖励配置设置,这在某些情况下会有所帮助.这是我的工作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>
Run Code Online (Sandbox Code Playgroud)

特别要注意,ExtensionlessUrlHandler-Integrated-4.0path属性设置**.(例如).