3

I have a .Net core 2.2 WebAPI that works perfectly fine with "normal" style URLs e.g. /api/controller/action/param etc. I have a 3rd party service that does a POST to this API with a URL encoded path and the Asp.Net routing fails to route this request correctly.

The controller is at: .../api/file/uploadFile/{filename}

The POST from the 3rd party is: ".../api%2Ffile%2FuploadFile%2FMaintenanceReport_2019_08_05_17_11_10.html.gz".

Replacing the %2F in the path appears to work as expected: ".../api/file/uploadFile/MaintenanceReport_2019_08_05_17_11_10.html.gz"

The filename is: "MaintenanceReport_2019_08_05_17_11_10.html.gz"

Placing a Route Attribute with %2F instead of "/" sort of works, but looks very messy.

The filename passed into the path is also not resolving correctly as a parameters. I suspect this is due to the file extension being included.

I've searched the net and did not find anything related jumping out at me. Any suggestions/ideas?

[Route("api/[controller]/[action]")]
[Route("api%2F[controller]%2F[action]")]
public class FileController : Controller
{
  ...
}

I would have thought that the .Net core routing engine would detect the path

1 Answer 1

2

The default path separator in the url generated by the route is / .The issue seems that the separator before the parameter which is as part of the path value is not recognized or missing .

If you request the url like .../api%2Ffile%2FuploadFile%2FMaintenanceReport_2019_08_05_17_11_10.html.gz , you could try to use URL Rewriting Middleware like below :

  1. In Configure

    app.UseRewriter(new RewriteOptions()
                .Add(RewriteRouteRules.ReWriteRequests)
                );
    

2.Custom a class containing ReWriteRequests

public class RewriteRouteRules
{
    public static void ReWriteRequests(RewriteContext context)
    {
        var request = context.HttpContext.Request;

        if (request.Path.Value.Contains("%2F", StringComparison.OrdinalIgnoreCase))
        {
            context.HttpContext.Request.Path = context.HttpContext.Request.Path.Value.Replace("%2F", "/");
        }

    }
}

Reference: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-2.1&tabs=aspnetcore2x

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this, fixed the issue for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.