简体   繁体   中英

Routing in dot net core with more than one parameter

Please let me know the exact method to ensure that correct parameters are sent in URL in order to navigate correctly in dot net core.

    [HttpGet("{id},{id2}",Name ="Edit")]
    [AllowAnonymous]
    public ActionResult Edit(int id, int id2)
    {
        return Ok(3);
    }

    [HttpGet("{id}")]
    public string Get(int id)
    {
        return "value";
    }

When i try navigating to following url:

/api/test/Edit?id=1&id2=4

it gets navigated to the other method Get and returns value as string.

Startup.cs file has the following contents

app.UseMvcWithDefaultRoute();

Ensure that the correct route templates are applied to the actions

[Route("api/[controller]")]
public class TestController: Controller {

    //GET api/test/edit?id=1&id2=4
    [HttpGet("Edit")]
    [AllowAnonymous]
    public ActionResult Edit(int id, int id2) {
        //...

        return Ok(3);
    }

    //GET api/test/5
    [HttpGet("{id}")]
    public string Get(int id) {
        return "value";
    }
}

Reference Routing to controller actions in ASP.NET Core

.Net Core 3.1.3

In the Startup.Configure Method:

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

Controller Annotation: [Route("[controller]/[action]")]

Action Method Annotation: [HttpGet("{param1:int}/{param2:int}")]

From the postman: https://localhost:5001/controller/action/param1/param2

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM