Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core routing to other method than my Name in HttpGet route

I have a simple controller in my ASP.NET core application. Idea is that the controller is called CreditData, and my endpoint could, for instance, be /api/creditdata/ and then it should have the default expected API methods.

I have two methods I want:

  • /api/creditdata and it should have a query such as /api/creditdata?query=text1,text2
  • /api/creditdata/value where a query such as /api/creditdata/text1 should work

I have tried to set it up like the following:

   [Route("api/[controller]")]
    public class CreditDataController : Controller
    {
        private CreditDataService _creditDataService;
        public CreditDataController()
        {
            _creditDataService = new CreditDataService();
        }

        // GET: api/CreditData?query=text1,text2
        [HttpGet("{query}", Name = "Get")]
        public List<CreditData> Get([FromQuery] string query)
        {
            // code
        }

        // GET: api/CreditData/GetByRegistration/33514322
        [HttpGet("{query}", Name= "GetByRegistration")]
        public CreditData GetByRegistration(string query)
        {
            // code
        }


    }

I have a pretty standard Startup.cs file:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
                {
                    HotModuleReplacement = true,
                    ReactHotModuleReplacement = true
                });
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

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

                routes.MapSpaFallbackRoute(
                    name: "spa-fallback",
                    defaults: new { controller = "Home", action = "Index" });
            });
        }

But neither works the way I want. I am used to the ASP.NET MVC 4/5 way, but logic seems different here.

How do set up the correct URL's?

like image 887
Lars Holdgaard Avatar asked Apr 06 '18 13:04

Lars Holdgaard


1 Answers

With [HttpGet("{query}", Name = "Get")], you're stating that the URL must be something like:

api/CreditData/query

What you actually want is just:

// GET: api/CreditData?query=text1,text2
[HttpGet(Name = "Get")]
public List<CreditData> Get([FromQuery] string query)

Providing {query} in the HttpGet attribute is specifying that it must be part of the route, which is not what you want, so I've removed it.

Your second [HttpGet] is for exactly the same route. For this, you want:

// GET: api/CreditData/GetByRegistration/33514322
[HttpGet("GetByRegistration/{query}", Name= "GetByRegistration")]
public CreditData GetByRegistration(string query)

Here, I've added GetByRegistration to show that it's a static part of the route. In this case, your query parameter will be populated from the route due to the rule I mentioned above.

Note: Unless you're referencing the routes by name elsewhere, you don't need the Name properties.

like image 164
Kirk Larkin Avatar answered Sep 28 '22 03:09

Kirk Larkin