1

I have the following code:

[Produces("application/json")]
[Route("api/[controller]")]
public class MarketReportInstancesTableController : BaseController
{
    internal readonly MyIRIntegrationDbContext Context;

    public MarketReportInstancesTableController(ILogger<MarketReportInstancesTableController> logger,
        MyIRIntegrationDbContext context) : base(logger)
    {
        Context = context;
    }

    [HttpGet (Name ="PageData")]
    public IActionResult PageData([FromQuery] IDataTablesRequest request)
    {
    .... methd body in here
    }

And I try to access with a URL like:

http://somehost/pca/api/MarketReportInstancesTable/pagedata

Which DOES NOT work, but

http://somehost/pca/api/MarketReportInstancesTable/

DOES WORK.

My question would be, why does the route do that? I want to have many paths in the same WebAPI controller.

Am I approaching it wrong?

0

1 Answer 1

1

You have no route template in the route. You only have a route name

Route names can be used to generate a URL based on a specific route. Route names have no impact on the URL matching behavior of routing and are only used for URL generation. Route names must be unique application-wide.

emphasis mine

//GET api/MarketReportInstancesTable/pagedata
[HttpGet ("pagedata", Name ="PageData")]
public IActionResult PageData([FromQuery] IDataTablesRequest request) {
    //.... methd body in here
}

Using [HttpGet] without a route template is the same as [HttpGet("")] which will map to the root of the controller with route prefix.

This explains why your root call works.

Reference Routing in ASP.NET Core

Reference Routing to Controller Actions

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

1 Comment

Ahh so perfect, I had just convinced myself I didn't need that!

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.