Routing controller with optional Id not in parameters - c#

I'm currently struggling in a problem where I need to register a route like:
https://localhost:44300/oneController/{Guid}/method1?param1=10000&param2=stuff
What I've tried was something similar to this:
[HttpGet]
public async Task<ActionResult> method1(Guid id, int param1, string param2)
{
...
}
However, this does not seems to work, any ideas to resolve this issue?

You can use RouteAttribute, like
[Route("{id:guid}/method1"), HttpGet]
public async Task<ActionResult> method1(Guid id, int param1, string param2)
{
...
}
Good to read MSDN Documentation: Attribute Routing in ASP.NET

Related

Swagger - same route, both GET method, swagger 2.0 not supported

Hiiiiiii.
I have 2 methods with the form show below:
Swagger showed me 500 error. Not supported.
[System.Web.Http.AllowAnonymous]
[System.Web.Http.HttpGet]
[System.Web.Http.Route("api/some/saveTemplate/1.1/action")]
public async Task<IHttpActionResult> SaveSomething(string param1, string param2)
{
//some.code();
return Ok(json);
}
[System.Web.Http.AllowAnonymous]
[System.Web.Http.HttpGet]
[System.Web.Http.Route("api/some/saveTemplate/1.1/action")]
public async Task<IHttpActionResult> SaveSomething(string param1, string param2, int param3)
{
//some.code();
return Ok(json);
}
As you can see above, i just have an overload on the method.
How can i get the solution? I cannot change any from my endpoints/routes. I am supporting a system in production, but I need to generate documentation.
can i generate any type of exclusion to swagger get the first method as a "GET 1" an the second as a "GET 2" or similar?
thank you very much

Handle WebApi Core methods with Id as string and int

I have two methods:
[HttpGet("{id}")]
public IActionResult GetTask([FromRoute] int id)
{
}
[HttpGet("{userId}")]
public IActionResult GetUserTask([FromRoute] string userId)
{
}
As you can see, i want to pass to my API routes like:
https://localhost:44365/Task/1
and
https://localhost:44365/Task/string
But my WebApi project cant handle it. When i pass route like this:
https://localhost:44365/Task/7dd2514618c4-4575b3b6f2e9731edd61
i get an 400 http and this response:
{
"id": [
"The value '7dd2514618c4-4575b3b6f2e9731edd61' is not valid."
]
}
While debugging, im not hitting any methods (when i pass string instead of int)
My question is, how to verload methods with one parameters with string or int? These methods do diffrent things
EDIT
When i pass something like:
https://localhost:44365/Task/dddd
I still get response with invalid id
You can define parameter type like [HttpGet("{id:int}")]. For more information refer below link.
https://learn.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#route-constraints
Your actions should be like below.
[HttpGet("{id:int}")]
public IActionResult GetTask([FromRoute] int id)
{
}
[HttpGet("{userId}")]
public IActionResult GetUserTask([FromRoute] string userId)
{
}
Use like this
[HttpGet("{id}")]
public IActionResult GetTask([FromRoute] int id)
{
}
[HttpGet("User/{userId}")]
public IActionResult GetUserTask([FromRoute] string userId)
{
}
and while calling api with guid/string use
https://localhost:44365/Task/User/7dd2514618c4-4575b3b6f2e9731edd61

Handling multiple IDs in Route

As far as I can understand, the conventional routing in .NET Core MVC is [controller]/[action]/{id?}
However, I have the following POST request I'm trying to catch which looks like this:
myDomain/MyController/MyAction/userID/anotherID/myInfo
I have tried the following, but it doesn't seem to be working:
public class MyController : Controller
{
[HTTPPost]
[Route("MyAction/{userID:minlength(2)}/{anotherID:int}/myInfo")]
public IActionResult MyAction([FromRoute] string userID, [FromRoute] int anotherID, [FromBody] string stuffIWant)
{
return Ok();
}
}
Obviously I'm not handling the routing correctly, but I'm not sure how I would get userID and anotherID from that route. I've published this action to my site, and tried to do a test post with the same URL, but didn't get a response.
Change to:
public class MyController : Controller
{
[HTTPPost]
[Route("MyAction/{userID:minlength(2)}/{anotherID:int}/myInfo")]
public IActionResult MyAction(string userID, int anotherID, [FromBody] string stuffIWant)
{
return Ok();
}
}

FromUri into class

I'm using this code
[Route("{id}/users/{name}"]
[HttpGet]
public string GetUserInfo([FromUri]int id, string name)
{
return this.GetInfo(new User {Id = id, Name = name});
}
Can I use a ([FromUri]Someclass class) in c# in Route like this:
[Route("{id}/users/{name}"]
[HttpGet]
public string GetUserInfo([FromUri]User user)
...
The question is "How can I use [FromUri](or something else)Someclass in thar Route
You can also use [FromBody] attribute to bind the parameter class from the request body.
[Route("{id}/users/{name}"]
[HttpGet]
public string GetUserInfo([FromBody]User user)
...
You can read more about this here.

How to support both 'regular' and 'readable' urls in Web API attribute rouring?

I have ASP.NET Web API 2.1 project with attribute routing enabled, and a controller action decorated as following:
[Route("api/product/barcode/{barcodeType}/{barcode}")]
public async Task<IHttpActionResult> GetProduct([FromUri] BarcodeSearchCriteria searchCriteria)
where BarcodeSearchCriteria is a complex type:
public class BarcodeSearchCriteria
{
public string Barcode { get; set; }
public string BarcodeType { get; set; }
}
It works well for a 'regular' url like this:
/api/product/barcode/EAN/0747599330971
but how in the same time support an url like this:
/api/product/barcode/?barcodeType=EAN&barcode=0747599330971
I used to use it in my *.webtest before switched to 'readable` mode.
you could have 2 routes in this case:
[Route("api/product/barcode")] //expects values from query string
[Route("api/product/barcode/{barcodeType}/{barcode}")] //expects value from route
public async Task<IHttpActionResult> GetProduct([FromUri] BarcodeSearchCriteria searchCriteria)
It looks like there is no route defined for the regular Url with query string parameters.
Try making the route parameters optional like this.
[Route("api/product/barcode/{barcodeType=""}/{barcode=""}")]
public async Task<IHttpActionResult> GetProduct([FromUri] BarcodeSearchCriteria searchCriteria)
So it should also match the route template api/product/barcode route.
Haven't tested, though hope you got my point.

Categories