Hello guys
I'am currently working in Xamarin and got stucked with basic operation. Iam trying to call my NetCore web API from Xamarin using Refit.
The main problem is - I guess - that i want to pass object in paramater of call. The second problem could be that object has a List of enums as property.
Models:
public class ArticleFilter
{
public List<ArticleType> Types { get; set; }
public int? CarBrandId { get; set; }
}
public enum ArticleType
{
News = 1,
SpecialOffer = 2,
Service = 3,
}
Refit Interface:
[Headers("Content-Type: application/json")]
public interface IArticleApi
{
[Get("/api/articles")]
[Headers("Content - Type: application / json")]
Task<List<Article>> GetAll(ArticleFilter filter);
[Get("/api/articles/{id}")]
Task<ArticleDetail> GetOne(int id);
}
Api controller:
[HttpGet]
public async Task<IActionResult> GetAll([FromUri] ArticleFilterDto filter)
{
var articles = await _articleSectionService.GetAll(filter);
if (articles == null)
{
return NotFound();
}
return Ok(articles);
}
Postman call:

Test calls from Xamarin view model:
var filter = new ArticleFilter()
{
Types = new List<ArticleType> { ArticleType.News }
};
var test = new List<Article>();
test = await response.GetAll(filter);
Actually this one falls in exception - 415 - Unsupported media type
I came across their GitHub documentation but still no progress. Could some Superman help me please?
Finally solved
Just in case anyone ever will be in same situation. In my case changing from [Body]/[FromUri] to [Query] in Interface and [FromQuery] in controller method solved my problem.
[Headers("Content-Type: application/json")]
public interface IArticleApi
{
[Get("/api/articles/")]
Task<List<Article>> GetAll([Query]ArticleFilterDto filter);
}
[HttpGet]
public async Task<IActionResult> GetAll([FromQuery]ArticleFilterDto filter)
{
var articles = await _articleSectionService.GetAll(filter);
if (articles == null)
{
return NotFound();
}
return Ok(articles);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With