使用服务堆栈 3.9.21。在 IIS 7.5 中托管应用程序。使用集成的 4.0.30319 ASP.NET 应用程序池。
出于说明目的,假设我有以下路径(由 DTO 和相应的请求处理程序支持):
/cars/{id}
有些汽车的 id 中包含我认为必须进行 URL 编码的字符 - 管道 | 就是其中之一。当我发送请求以获取例如 id 为 '1|2' 的汽车时,我得到了一个很好的旧 500,并带有以下堆栈跟踪:
[ArgumentException]: Illegal characters in path.
at System.IO.Path.CheckInvalidPathChars(String path)
at System.IO.Path.GetFileName(String path)
at ServiceStack.MiniProfiler.UI.MiniProfilerHandler.MatchesRequest(IHttpRequest request) in C:\src\ServiceStack\src\ServiceStack\MiniProfiler\UI\MiniProfilerHandler.cs:line 24
at ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated) in C:\src\ServiceStack\src\ServiceStack\WebHost.Endpoints\ServiceStackHttpHandlerFactory.cs:line 153
at System.Web.HttpApplication.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
无论我是否对路径中的管道进行 URL 编码,我都会得到这个。我怎么解决这个问题?
更多细节
DTO
using ServiceStack.ServiceHost;
namespace SSUEB
{
[Route("/cars/{id}", "GET")]
public class ById
{
public string Id { get; set; }
}
}
端点
using System.Collections.Generic;
using System.Net;
using ServiceStack.Common.Web;
using Stack = ServiceStack.ServiceInterface;
namespace SSUEB
{
public class Cars : Stack.Service
{
public object Get(ById byId)
{
return new HttpResult(new Dictionary<string, string> {{"id", byId.Id}}, HttpStatusCode.OK);
}
}
}
应用程序
using ServiceStack.WebHost.Endpoints;
namespace SSUEB
{
public class SSUEB : AppHostBase
{
public SSUEB() : base("ServiceStack toy service", typeof(SSUEB).Assembly)
{
}
public override void Configure(Funq.Container container)
{
ServiceStack.Text.JsConfig.EmitCamelCaseNames = true;
SetConfig(new EndpointHostConfig { DebugMode = true });
}
}
}
要求
这个:
curl -v -X GET -H "Accept: application/json" http://localhost:80/dealership/cars/123%20456
获得预期的 200 响应:
{"id":"123 456"}
但是这个:
curl -v -X GET -H "Accept: application/json" http://localhost:80/dealership/cars/123%7C456
导致最初报告的问题。
IIS 请求日志显示 %7C 被解码为管道并且 %20 被解码(在日志中至少作为一个加号)。后者导致 200(OK)响应,前者 - 500(内部服务器错误)。
2012-12-03 22:21:20 127.0.0.1 GET /dealership/cars/123|456 - 80 - 127.0.0.1 curl/7.27.0 500 0 0 5
2012-12-03 22:21:25 127.0.0.1 GET /dealership/cars/123+456 - 80 - 127.0.0.1 curl/7.27.0 200 0 0 1