I have an API endpoint that accepts an enum. However, the property is coming in as the EnumMemberValue. I want to convert this to the correct enum.
public enum MyEnum
{
[EnumMember(value="Test One")]
Test1
. . .
}
[HttpGet]
public async Task<IActionResult> Get(MyEnum e)
{
. . .
}
With a param in my request looking like ?e=Test%20One, it doesn't deserialize to the correct enum
Related
I have this controller
[Route("api/controller/method/{a}/{b}")]
public string Get(string a,string b)
{
return a+"__"+b;
}
It works only when I send this type of request in browser
api/controller/method/a/b
How can I modify controller so I could send such type of request:
api/controller/method?a=a&b=b
As I understood from the example you showed, you want to use query format to pass the values. You can achieve this like this:
[HttpGet("api/controller/method")]
public string Get([FromQuery]string a, [FromQuery]string b)
{
return a+"__"+b;
}
I have a controller where I get the ID from the route.
[HttpGet]
[Route("{vehicleId}")]
public InfoDto GetInfo([FromUri] VehicleDetailsRequest request)
{
return ...;
}
The VehicleDetailsRequest object looks like this (the Validator is from FluentValidation):
[Validator(typeof(VehicleDetailsRequestValidator))]
public class VehicleDetailsRequest
{
public int VehicleId { get; set; }
public string Lang { get; set; }
}
I can query this action as I expect with http://localhost/controller/123?lang=sv but my swagger documentation looks like this:
How can I get Swashbuckle/Swagger to only show me one vehicleId but still keep the FluentValidation?
I'm using Swashbuckle 5.6 and .Net Framework 4.6.2.
In my opinion, there is something wrong with action method signature.
The parameter from the URL should be present in the action method parameters.
Also, if you want only lang in query string, then it should also be present as a parameter in the action.
Please note that if you specify object in the action parameters, all its properties would be forming the query string.
So, your action method should like below:
[HttpGet]
[Route("{vehicleId}")]
public InfoDto GetInfo(int vehicleId, [FromUri] string lang)
{
return ...;
}
This should help you to resolve the issue.
Is it possible to have 2 methods name with same name in WebApi and same parameter type but different parameter?
For example one is get Product by Yearid
And the other one is get Product by Productid
And I like to have this Rout:
Products?yearId=10
Products/15
I know I can have different name but my boss likes to have same name I wonder if it is possible.
and these are methods:
[HttpGet]
[Route("Products/{yearId}")]
public async Task<IEnumerable<Make>> GetProductsYearId(int yearId)
{
....
}
[HttpGet]
[Route("Products/{makeid}")]
public async Task<Make> GetProductById(int makeid)
{
.....
}
Not sure how the [Route] should looks like to get this end result.
As Brendan Green commented, you would need to have two separate routes defined. Otherwise there wouldn't be a way to determine which method you actually intended to invoke:
[HttpGet]
[Route("Products/Year/{yearId}")]
public async Task<IEnumerable<Make>> GetProductsYearId(int yearId)
{
...
}
[HttpGet]
[Route("Products/Make/{makeid}")]
public async Task<Make> GetProductById(int makeid)
{
...
}
I have the following Controller code:
namespace PlatypusReports.Controllers
{
[RoutePrefix("api/platypus")]
public class PlatypusController : ApiController
{
[Route("{unit}/{begindate}")]
[HttpPost]
public void Post(string unit, string begindate)
{
. . .
}
[Route("{unit}/{begindate}")]
[HttpGet]
public string Get(string unit, string begindate)
{
. . .
}
. . .
Calling the POST method works, but calling the GET method does not; in the latter case, I get, "405 Method Not Allowed - The requested resource does not support http method 'GET'."
I call them with the same exact URL from Postman:
http://localhost:52194/api/platypus/poisontoe/201509
...the only difference being I select "POST" it works, and when I select "GET" it doesn't.
Why would POST work but not GET? What do I have to change in my GET code to get it to be supported/allowed?
If you are using an apicontroller you don't need the decorators HttpPost and HttpGet. If you remove them it should then work.
I am using the new asp.net web api and would like to pass optional parameters. Is it correct that one needs to populate an attribute so it allows one to pass params using the ? symbol?
Before this was done with with uri templates, I believe.
Does anyone have an example?
I am currently passing the id in the url which arrives in my controller as int. But I need to pass some dates.
You can make a parameter optional by using a nullable type:
public class OptionalParamsController : ApiController
{
// GET /api/optionalparams?id=5&optionalDateTime=2012-05-31
public string Get(int id, DateTime? optionalDateTime)
{
return optionalDateTime.HasValue ? optionalDateTime.Value.ToLongDateString() : "No dateTime provided";
}
}
In addition to the previous answer provided by Ian, which is correct, you can also provide default values which I feel is a cleaner option which avoids having to check whether something was passed or not. Just another option.
public class OptionalParamsController : ApiController
{
// GET /api/optionalparams?id=5&optionalDateTime=2012-05-31
public string Get(int id, DateTime optionalDateTime = DateTime.UtcNow.Date)
{...}
}