点字符“。”在MVC的Web API 2的要求,如API /人/ STAFF.45287 [英] Dot character '.' in MVC Web API 2 for request such as api/people/STAFF.45287

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

问题描述

我试图让作品的网址之一的风格: http://somedomain.com/api /people/staff.33311 (就像为LAST.FM允许所有排序在他们的基于REST和放大器的迹象场所;网页网址,例如的http://www.last.fm/artist/psy'aviah 是LAST.FM有效的URL)。

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).

什么作品有以下情况:
- http://somedomain.com/api/people/ - 返回所有的人
- http://somedomain.com/api/people/staff33311 - 也行,但它不是我'米后
我想要的网址接受一个点,如下面的例子
- http://somedomain.com/api/people/staff.33311 - 但是这给了我一个

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. 控制器把PeopleController

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


  • 在WebApiConfig.cs

  • 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 }
        );
    }
    


  • 我已经尝试过按照本博文的所有提示<一个href=\"http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx\">http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx但它仍然是行不通的。我也觉得这是相当繁琐的,我不知道如果没有其他更好的和更安全的方式。

    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.

    我们有我们的ID在内部这样的,所以我们将不得不找到一个解决方案,以适应圆点这种或那种方式,preferably中的风格​​。但我愿意为网址的替代建议,如果需要的话...

    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...

    推荐答案

    继在你的的web.config 文件应该解决您的问题设置:

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

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

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

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