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

 手机用户2502875355 发布于 2023-02-05 10:35

我试图让工作的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.

我已经设置了以下内容:

    控制器"PeopleController"

    public IEnumerable 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);
    }    
    

    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,因此我们必须找到一种解决方案,以这种或那种方式适合点,最好是"."的风格.但如果需要,我愿意接受网址的其他建议......

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

    2023-02-05 10:37 回答
  • 我发现在标准之前添加以下内容可以解决我的问题:ExtensionlessUrlHandler

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

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

    H/T到/sf/ask/17360801/

    2023-02-05 10:37 回答
  • 在您的web.config文件中进行以下设置后,应解决以下问

    <configuration>
        <system.webServer>
            <modules runAllManagedModulesForAllRequests="true" />
    

    2023-02-05 10:37 回答
  • 我发现我需要做的不仅仅是设置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>
    

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

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

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

    2023-02-05 10:40 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有