.net core,如何处理带有额外前导斜杠的路由
.net core , how to handle route with extra leading slash
我需要处理以下形式的传入请求:
//ohif/study/1.1/系列
注意前面的 exta 斜线
我的控制器签名是:
[Route("ohif/study/{studyUid}/series")]
[HttpGet]
public IActionResult GetStudy(string studyUid)
如果我将传入请求修改为 /ohif/study/1.1/series,它工作正常
但是当我使用 //ohif/study/1.1/series 时,路由没有命中
另外我也试过:[Route(/ohif/study/{studyUid}/series")]
和 [Route("//ohif/study/{studyUid}/series")]
都失败了。不幸的是,我无法更改传入请求,因为它来自外部应用程序。有什么技巧可以处理这条路线吗?我在 .NET Core 3.0 中工作。
更新说明:
我已激活日志记录,我看到 asp.net 核心正在分析路由,我收到消息:
找不到请求路径“//ohif/study/1.1/series”的候选项
对于记录器 Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware
在 Web 服务器级别重写 URL,例如对于 IIS,您可以使用 URL 重写模块自动将 //ohif/study/1.1/series
重定向到 /ohif/study/1.1/series
。这不是您申请的工作。
处理双斜杠的中间件怎么样?
app.Use((context, next) =>
{
if (context.Request.Path.Value.StartsWith("//"))
{
context.Request.Path = new PathString(context.Request.Path.Value.Replace("//", "/"));
}
return next();
});
我需要处理以下形式的传入请求: //ohif/study/1.1/系列 注意前面的 exta 斜线
我的控制器签名是:
[Route("ohif/study/{studyUid}/series")]
[HttpGet]
public IActionResult GetStudy(string studyUid)
如果我将传入请求修改为 /ohif/study/1.1/series,它工作正常
但是当我使用 //ohif/study/1.1/series 时,路由没有命中
另外我也试过:[Route(/ohif/study/{studyUid}/series")] 和 [Route("//ohif/study/{studyUid}/series")]
都失败了。不幸的是,我无法更改传入请求,因为它来自外部应用程序。有什么技巧可以处理这条路线吗?我在 .NET Core 3.0 中工作。
更新说明: 我已激活日志记录,我看到 asp.net 核心正在分析路由,我收到消息: 找不到请求路径“//ohif/study/1.1/series”的候选项 对于记录器 Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware
在 Web 服务器级别重写 URL,例如对于 IIS,您可以使用 URL 重写模块自动将 //ohif/study/1.1/series
重定向到 /ohif/study/1.1/series
。这不是您申请的工作。
处理双斜杠的中间件怎么样?
app.Use((context, next) =>
{
if (context.Request.Path.Value.StartsWith("//"))
{
context.Request.Path = new PathString(context.Request.Path.Value.Replace("//", "/"));
}
return next();
});