We recently converted an old Nancy Web api project to ASP.NET Core Web api project.
Unfortunately our client nuget which is already used by a few other teams, has a bug in a few of the urls that it generate where it has a double '/' after the domain, for example:
http://domain//controller/{property}
Notice the double '/' after the domain.
How can I configure an endpoint to accept both the correct route as well as the incorrect route?
I have tried to prefix the '/' once and twice into the [HttpPost] attribute, but it is ignored:
[ApiController]
public class FirmwareController : ControllerBase
{
[HttpPost("//firmware/{deviceType}")]
public async Task<IActionResult> TestAction(string deviceType)
{
return Ok();
}
}
I have also tried to call app.MapGet(), but this causes an error.
app.MapGet("//firmware/{deviceType}", (string deviceType) =>
{
});
The error:
Microsoft.AspNetCore.Routing.Patterns.RoutePatternException: 'The route template separator character '/' cannot appear consecutively. It must be separated by either a parameter or a literal value.'
Is there way to allow and configure this? I would like to support both the correct and the incorrect urls so that we can ease the transition to the new Api and make it easier for the other teams.
You can use URL rewriting to mitigate this sort of issue.
var options = new RewriteOptions()
.AddRewrite(@"^\/\/(.*)", "/$1");
app.UseRewriter(options);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments