我试图让工作的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 IEnumerableGetAllPeople() { 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,因此我们必须找到一种解决方案,以这种或那种方式适合点,最好是"."的风格.但如果需要,我愿意接受网址的其他建议......
使用斜杠http://somedomain.com/api/people/staff.33311/
替换URL,例如而不是http://somedomain.com/api/people/staff.33311
.
我发现在标准之前添加以下内容可以解决我的问题: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/
在您的web.config
文件中进行以下设置后,应解决以下问
<configuration> <system.webServer> <modules runAllManagedModulesForAllRequests="true" />
我发现我需要做的不仅仅是设置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.0
其path
属性设置*
为*.
(例如).
我不知道我到底在做什么,但在使用之前的答案后,我想出了另一个,也许更合适的解决方案:
<system.webServer> <modules> <remove name="UrlRoutingModule-4.0" /> <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" /> </modules> </system.webServer>