问题
I need to handle an incoming request which is of the form: //ohif/study/1.1/series Note the exta slash at the front
My controller signature is:
[Route("ohif/study/{studyUid}/series")]
[HttpGet]
public IActionResult GetStudy(string studyUid)
If I modify the incoming request to /ohif/study/1.1/series it works fine
however when I use //ohif/study/1.1/series, the route is not hit
Additionally I also tried: [Route("/ohif/study/{studyUid}/series")] and [Route("//ohif/study/{studyUid}/series")]
Both fail. I unfortunately cannot change the incoming request as it is from an external application. Is there some trick to handle this route? I am working in .NET Core 3.0.
Update NOTE: I have logging activated and I see that asp.net core is analyzing the route, I have the message: No candidates found for the request path '//ohif/study/1.1/series' for the logger Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware
回答1:
Rewrite the URL at the web server-level, e.g. for IIS, you can use the URL Rewrite Module to automatically redirect //ohif/study/1.1/series
to /ohif/study/1.1/series
. This isn't a job for your application.
回答2:
What about the middleware to handle double slash?
app.Use((context, next) =>
{
if (context.Request.Path.Value.StartsWith("//"))
{
context.Request.Path = new PathString(context.Request.Path.Value.Replace("//", "/"));
}
return next();
});
来源:https://stackoverflow.com/questions/59304132/net-core-how-to-handle-route-with-extra-leading-slash