GilShalit
GilShalit

Reputation: 6473

.net core 3.1: Endpoint routing for razor pages AND API controllers

**Edit - added query string

I have upgraded a Core 2.2 site to 3.1, and the only issue still bugging me is the following: I have regular Razor pages and API controllers in the same application - shared code and functionality make this an easy solution. In 2.2, there wasn't an issue with the following routing setup:

services.AddMvc()

and

 app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

in Startup.ConfigureServices and Startup.Configure, respectively.

My controller looks like:

{
    [Route("api/[controller][")]
    [Produces("application/json")]
    [ApiController]
    public class RRateController : ControllerBase
    {
        public RRateController()
        {
        }

        [HttpGet]
        public async Task<clsObject> Get([FromQuery] string[] TopCodes)
        {
            clsObject obj = new clsObject();
            ...
            return obj;
        }
}

The controller is called with the query string:

Root + '/API/RRate?TopCodes=Val1&TopCodes=Val2'

Upgrading to .net core 3.1, I am using

    services.AddControllers();
    services.AddRazorPages();

and

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapRazorPages();
        endpoints.MapControllers();
    };

in ConfigureServices and Configure.

My problem is that the string array for TopCodes is not passed into the controller. The controller is accessed but the array is empty. A similar controller with no parameters works fine with the new setup.

How should I configure endpoints to work both with Razor pages with the Controller\Action\parameter pattern, and for the API controllers?

Upvotes: 0

Views: 2012

Answers (1)

Shoejep
Shoejep

Reputation: 4849

Was the [ in api/[controller][ intentional? Shouldn't it be api/[controller]?

Upvotes: 2

Related Questions