点字符'.'在 MVC Web API 2 中用于请求,例如 api/people/STAFF.45287 [英] Dot character '.' in MVC Web API 2 for request such as api/people/STAFF.45287

查看:23
本文介绍了点字符'.'在 MVC Web API 2 中用于请求,例如 api/people/STAFF.45287的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用的 URL 是以下样式的 URL:http://somedomain.com/api/people/staff.33311(就像 LAST.FM 这样的网站允许在其 RESTFul 和网页网址中使用各种标志,例如http://www.last.fm/artist/psy'aviah" 是 LAST.FM 的有效网址.

适用于以下场景:- http://somedomain.com/api/people/ - 返回所有人- http://somedomain.com/api/people/staff33311 - 也可以,但这不是我的米后我希望 url 接受一个点",就像下面的例子- http://somedomain.com/api/people/staff.33311 - 但这给了我一个 >

HTTP 错误 404.0 - 未找到您要查找的资源已被删除、更名或暂时不可用.

我设置了以下内容:

  1. 控制器PeopleController"

    public IEnumerable获取所有人(){返回_人;}公共 IHttpActionResult GetPerson(string id){var person = _people.FirstOrDefault(p => p.Id.ToLower().Equals(id.ToLower()));如果(人==空)返回 NotFound();返回确定(人);}

  2. WebApiConfig.cs

    public static void Register(HttpConfiguration config){//Web API 配置和服务//Web API 路由config.MapHttpAttributeRoutes();config.Routes.MapHttpRoute(name: "DefaultApi",routeTemplate: "api/{controller}/{id}",默认值:新 { id = RouteParameter.Optional });}

我已经尝试过遵循这篇博文的所有提示http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsOtheringInsLacks. 但它仍然无法工作.. 我也认为这很乏味,我想知道是否还有另一种更好、更安全的方法.

我们的 Id 在内部是这样的,所以我们将不得不找到一种解决方案来以一种或另一种方式来适应点,最好是."的风格.但如果需要,我愿意接受其他 url 建议......

解决方案

web.config 文件中的以下设置应该可以解决您的问题:

<预><代码><配置><system.webServer><modules runAllManagedModulesForAllRequests="true"/>

The URL I'm trying to let work is one in the style of: http://somedomain.com/api/people/staff.33311 (just like sites as LAST.FM allow all sort of signs in their RESTFul & WebPage urls, for example "http://www.last.fm/artist/psy'aviah" is a valid url for LAST.FM).

What works are following scenarios: - http://somedomain.com/api/people/ - which returns all people - http://somedomain.com/api/people/staff33311 - would work as well, but it's not what I'm after I'd want the url to accept a "dot", like the example below - http://somedomain.com/api/people/staff.33311 - but this gives me a

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

I've set up following things:

  1. The 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. The 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 }
        );
    }
    

I already tried following all the tips of this blogpost http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx but it still won't work.. I also think it's quite tedious and I wonder if there isn't another, better and more secure way.

We have our Id's internally like this, so we're going to have to find a solution to fit the dot in one way or another, preferably in the style of "." but I'm open to alternative suggestions for urls if need be...

解决方案

Following setting in your web.config file should fix your issue:

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

这篇关于点字符'.'在 MVC Web API 2 中用于请求,例如 api/people/STAFF.45287的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆