How to allow a double '/' in a route of an ASP.NET Core Web Api action?

Martin Lottering

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.

Octavian Mărculescu

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.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.NET CORE, Web API: No route matches the supplied values

Mix web api action and non api action same controller asp.net core

How to unit test an action filter attribute for web api in asp.net core?

How to handle different action with same route and different authorize attribute in asp .net core

ASP.Net Core Web API Controller action with many parameters

ASP.NET Core route of a controller and the index action

How to change the default controller and action in ASP.NET Core API?

passing an array to a asp net core web api action method HttpGet

How to make ASP.NET Core Web API action execute asynchronously?

ASP.Net Core Web API application : How to return success from all my action methods and not run the code in a certain mode?

Can't map route to action, ASP.NET Core Web API

ASP.NET Core - Cache problems with SPA route in Web API

ASP.NET Web API 2 override inherited attribute route action multiple actions found

ASP.Net Core Web API custom route not working

How to send forms' data to ASP.NET Core Web API action decorated with [FromForm] in Angular?

How to allow HTTPS connections from both localhost and container towards an ASP.NET Core Web API application?

Set action name as route attribute automatically in ASP.NET Web API 2

ASP.Net Web API Action Result

Route-specific ASP.NET MVC Web API message handler not invoking controller action

.net core asp.net web api action returning unexpected status code

Change the default route in Asp.net core Web Api

Action routing in .Net Core 3 Web API

CORS issue asp.net core web api. No 'Access-Control-Allow-Origin' header is present

How to secure ASP.NET Core Web API endpoints to only allow requests from React application?

How do I disable automatic model binding for a specific ASP.NET Core 5.0 Web API action?

Attaching middleware to a specific route in ASP.NET Core Web API?

ASP.NET Core Web API : route by query parameter

ASP.NET CORE API route authotization Policy should allow policy1 OR policy2

ASP.NET Core Web API - InvalidOperationException: Body was inferred but the method does not allow inferred body parameters