gpt4 book ai didi

c# - MVC Web API 2 中的点字符 '.' 用于 api/people/STAFF.45287 等请求

转载 作者:IT王子 更新时间:2023-10-29 03:36:07 25 4
gpt4 key购买 nike

我尝试使用的 URL 格式如下:http://somedomain.com/api/people/staff.33311 (就像 LAST.FM 这样的网站允许在其 RESTFul 和网页 url 中使用各种符号,例如“http://www.last.fm/artist/psy'aviah”是 LAST.FM 的有效 url)。

以下场景有效:- http://somedomain.com/api/people/ - 返回所有人- http://somedomain.com/api/people/staff33311 - 也可以,但这不是我想要的我希望 url 接受一个“点”,如下例- 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. Controller “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 的建议......

最佳答案

用斜杠作为 URL 的后缀,例如http://somedomain.com/api/people/staff.33311/而不是 http://somedomain.com/api/people/staff.33311 .

关于c# - MVC Web API 2 中的点字符 '.' 用于 api/people/STAFF.45287 等请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20998816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com