Skip to content

UsePathBase() duplicates route paths #47723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
QuantumToasted opened this issue Apr 15, 2023 · 1 comment
Closed
1 task done

UsePathBase() duplicates route paths #47723

QuantumToasted opened this issue Apr 15, 2023 · 1 comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates

Comments

@QuantumToasted
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

After some research and testing to find a way to globally prefix my API routes with /api, I have arrived at the following code:

var apiUrl = app.Configuration["RemotePath:Api"]!;
app.UsePathBase(!string.IsNullOrWhiteSpace(apiUrl) ? apiUrl : null);
app.UseRouting();

This works great, and now a endpoint method mapped to /foo/bar is actually mapped to /api/foo/bar. However...it seems there still exists a mapping for /foo/bar.

Take this example controller in a project I'm working on:

[Route("upload")]
public sealed class UploadController : Controller
{
    [HttpPut("oid/{oid}")]
    public async Task<IActionResult> UploadOid(string oid, [FromBody] LocalFileBlobAdapter.RemoteFileLocation location)
    {
        ...
        return NoContent();
    }
}

In Postman, I am able to make a PUT request to /api/upload/oid/{oid}, and /upload/oid/{oid}.
image
image

Expected Behavior

Using UsePathBase() should not preserve the original controller routing configuration (IE, what the routing would be if UsePathBase() had never been called).

Steps To Reproduce

app.UsePathBase("/api");
app.UseRouting();

Then add any applicable controllers. (I am using a Web API template project, so I'm unsure what other code is specific to my project and not just the defaults).

If needed I can create a brand new project and put it on GitHub.

Exceptions (if any)

N/A

.NET Version

7.0.102

Anything else?

JetBrains Rider, project is targeting net7.0. Referenced packages, if of any use:

<ItemGroup>
    <PackageReference Include="Estranged.Lfs.Api" Version="3.1.0" />
    <PackageReference Include="Estranged.Lfs.Authenticator.GitHub" Version="3.1.0" />
    <PackageReference Include="JWT" Version="10.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.2" />
    <PackageReference Include="Octokit.Webhooks.AspNetCore" Version="1.4.0" />
    <PackageReference Include="StackExchange.Redis" Version="2.6.104" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
@ghost ghost added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Apr 15, 2023
@QuantumToasted
Copy link
Author

Woops, I neglected to search through closed issues as well. Seems to be a duplicate of #45076 - it would be nice, if not already the case, to document this behavior, because it doesn't seem intuitive at all to me, that both endpoints function after using this middleware. I will look into what this new 7.0 route groups feature is and hope it can work for my use case.

@ghost ghost locked as resolved and limited conversation to collaborators May 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

No branches or pull requests

1 participant