REST API Custom Response query - rest

I am a bit new to REST API. I have the below controller created to mock an API Service called by a client code under Test. I need to return the response in JSON format as mentioned in the query and need some help to fix it.
Controller:
[HttpPost]
[ApiKeyAuth]
[ValidateModel]
[ProducesResponseType(typeof(Item), StatusCodes.Status201Created)]
public async Task<IActionResult> AddNewItem([FromBody] Item item)
{
var itemId = await _repo.AddItemAsync(item);
return CreatedAtAction(nameof(GetItemById), new { itemId, controller = "Example" },
itemId);
}
Contract:
public interface IExampleControllerRepository
{
Task<int> AddItemAsync(Item item);
}
Repo:
public class ExampleRepository : IExampleControllerRepository
{
private readonly ExampleDbContext _context;
public ExampleRepository(ExampleDbContext context) => _context = context;
public async Task<int> AddItemAsync(Item item)
{
_context.Add(item);
await _context.SaveChangesAsync();
return item.ItemId;
}
}
Expected positive response template:
{"response":{"status":0,"data":[{"id":"1234"}]}}

// Summary:
// Creates a Microsoft.AspNetCore.Mvc.CreatedAtActionResult object that produces
// a Microsoft.AspNetCore.Http.StatusCodes.Status201Created response.
//
// Parameters:
// actionName:
// The name of the action to use for generating the URL.
//
// routeValues:
// The route data to use for generating the URL.
//
// value:
// The content value to format in the entity body.
//
// Returns:
// The created Microsoft.AspNetCore.Mvc.CreatedAtActionResult for the response.
[NonAction]
public virtual CreatedAtActionResult CreatedAtAction(string? actionName, object? routeValues, [ActionResultObjectValue] object? value)
{
throw null;
}
The response body's template is related to the type of value, So if you want to get the response template like:
{"response":{"status":0,"data":[{"id":"1234"}]}}
You need to pass a value of a specific type instead of itemId.
Here is a simple demo.
public class Test
{
public List<response> responses { get; set; }
}
public class response
{
public int status { get; set; }
public List<test1> data { get; set; }
}
public class test1
{
public string Id { get; set; }
}
For testing convenience, I just hard code here.
[HttpPost]
[ApiKeyAuth]
[ValidateModel]
[ProducesResponseType(typeof(Item), StatusCodes.Status201Created)]
public async Task<IActionResult> AddNewItem([FromBody] Item item)
{
Test test = new Test()
{
responses = new List<response>()
{
new response()
{
status = 0,
data = new List<test1> {
new test1()
{
Id = "1234",
}
}
}
}
};
var itemId = await _repo.AddItemAsync(item);
return CreatedAtAction(nameof(GetItemById), new { Id = item.ItemId }, test);
}
Result

Related

An error occurred while updating the entries

I'm strugglish with adding feature for my controller. While adding new item, receving the error like: "An error occurred while updating the entries. See the inner exception for details."
I debugged it, and understood ProductDetailIs is null and here is the issue. But, can not figure out how to mend the problem.
Here is the DTO models:
public class WishlistItemDto
{
public int Id { get; set; }
public string CustomerId { get; set; }
public ProductDetailsDtoWithPrimaryImage ProductDetails { get; set; }
public int Quantity { get; set; }
}
public class WishListItemCreationDto
{
public string CustomerId { get; set; }
public int ProductDetailId { get; set; }
public int Quantity { get; set; }
}
Controller:
[HttpPost]
public async Task<IActionResult> Add(WishListItemCreationDto wishListItemDto)
{
var itemAdd = _mapper.Map<WishlistItemDto>(wishListItemDto);
var itemCreated = await _wishListItemService.AddAsync(itemAdd);
return CreatedAtAction(nameof(GetId), new { id = itemCreated.Id }, wishListItemDto);
}
Service:
public async Task<WishlistItemDto> AddAsync(WishlistItemDto item)
{
var entity = _mapper.Map<WishlistItem>(item);
await _wishListItemRepository.AddAsync(entity);
return _mapper.Map<WishlistItemDto>(entity);
}
Repository:
public async Task<WishlistItem> AddAsync(WishlistItem item)
{
await _context.Set<WishlistItem>().AddAsync(item);
await _context.SaveChangesAsync();
return item;
}
This line here:
var itemAdd = _mapper.Map<WishlistItemDto>(wishListItemDto);
your "wishListItemDto" is passed in as a 'WishListItemCreationDto' which contains only a ProductDetailsId. Automapper will have no way of knowing how to convert that into a ProductDetailsDtoWithPrimaryImage.
Typically for something like this where you pass an reference ID you would compose your entity by either populating a FK or loading the referenced entity. Your existing service and repository patterns will complicate your final solution. From what I can see from your example I'd look at creating an AddAsync method that accepts the WishListItemCreationDto:
public async Task<WishlistItemCreationDto> AddAsync(WishlistItemCreationDto item)
{
var entity = _mapper.Map<WishlistItem>(item);
var productDetails = _productDetailsRepository.GetById(item.ProductDetailsId);
entity.ProductDetails = productDetails;
await _wishListItemRepository.AddAsync(entity);
return _mapper.Map<WishlistItemDto>(entity);
}
Without the added abstraction complexity of the Service and Repository the add operation can be a whole lot simpler:
[HttpPost]
public async Task<IActionResult> Add(WishListItemCreationDto wishListItemDto)
{
// or better, use an injected dependency to the Context...
// TODO: add applicable exception handling.
using(var context = new AppDbContext())
{
var item = _mapper.Map<WishlistItem>(wishListItemDto);
var productDetails = context.ProductDetails.Single(x => x.ProductDetailsId == wishListItemDto.ProductDetailsId);
item.ProductDetails = productDetails;
context.SaveChanges();
return CreatedAtAction(nameof(GetId), new { id = itemCreated.Id }, wishListItemDto);
}
}

How to send IFormFile to Web API as part of ViewModel object

I have .NET Core Web App with the following controller:
[HttpPost]
public async Task<IActionResult> Update(StudentDetailsViewModel vm)
{
var tokenNo = HttpContext.Session.GetString("Token");
vm.ID = Convert.ToInt32(HttpContext.Session.GetString("StudentId"));
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenNo);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var putStudentUrl = _appSettings.Value.Apis.GSRTCApi.Url + _appSettings.Value.Apis.GSRTCApi.StudentsEndpoint + vm.ID;
var settings = new JsonSerializerSettings();
var stringData = JsonConvert.SerializeObject(vm);
var contentData = new StringContent(stringData, Encoding.UTF8, "application/json");
var response = await client.PutAsync(putStudentUrl, contentData); // contentData);
return RedirectToAction("Index", "Home");
}
The controller calls my Web API and everything works fine until I upload a file through my html form. When this happens the file is picked up in the IFormFile property of the StudentDetailsViewModel on the client side but when the API call is made the whole object is null. The API controller is:
[HttpPut("{id}")]
public async Task<IActionResult> Put(int? id, [FromBody]StudentViewModel student)
{
// API operations here
}
My suspicion is that I am not serializing the StudentDetailsViewModel object properly since I have a property IFormFile, which is an interface. However, I am not sure exactly how I need to customize the Json.Newsoft object.
For sending IFormFile, you need to use FromForm which is default when you remove FromBody and MultipartFormDataContent.
Here are complete steps:
Web App Model
public class StudentDetailsViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public IFormFile File { get; set; }
}
Web App Controller
public async Task<IActionResult> Update(StudentDetailsViewModel vm)
{
HttpClient client = new HttpClient();
var putStudentUrl = #"url";
byte[] data;
using (var br = new BinaryReader(vm.File.OpenReadStream()))
{
data = br.ReadBytes((int)vm.File.OpenReadStream().Length);
}
ByteArrayContent bytes = new ByteArrayContent(data);
MultipartFormDataContent multiContent = new MultipartFormDataContent();
multiContent.Add(bytes, "file", vm.File.FileName);
multiContent.Add(new StringContent(vm.Id.ToString()),"Id");
multiContent.Add(new StringContent(vm.Name), "Name");
var response = await client.PutAsync(putStudentUrl, multiContent);
return RedirectToAction("Index", "Home");
}
Web API Model
public class StudentViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public IFormFile File { get; set; }
}
Web API Controller
[HttpPut("{id}")]
public async Task<IActionResult> Put(int? id,StudentViewModel student)
{
using (var stream = new FileStream(#"file path", FileMode.Create))
{
await student.File.CopyToAsync(stream);
}
return Ok();
}
Pay attention to multiContent.Add(bytes, "file", vm.File.FileName);, the second parameter is the name for IFormFile field.

REST API - CreatedAtRoute method doesn't return a value

I'm building some REST API server in .NET Core and using Postman software to test it. I have a problem with POST method which doesn't return me any value ("Could not get any response") when I try to perform second Add operation on my DBContext class inside CreateUser method. My code :
UsersController :
[Produces("application/json")]
[Route("api/[controller]")]
public class UsersController : Controller
{
private readonly DBContext _context;
#region CONSTRUCTOR
public UsersController(DBContext context)
{
_context = context;
}
#endregion
#region HTTP GET
// GET: api/users || api/users?cardnr=xxx
[HttpGet]
public async Task<IActionResult> GetUsers(string cardNr)
{
if (String.IsNullOrEmpty(cardNr))
{
try
{
var users = await _context.Users.ToListAsync();
if (users.Any())
{
return Json(users);
}
else
{
return NotFound();
}
}
catch (Exception ex)
{
Helpers.ExceptionLogger.LogException(ex);
return StatusCode(500);
}
}
else
{
try
{
var user = await _context.Users.FirstOrDefaultAsync(u => u.Cards.Any(c => c.CardNumber.Equals(cardNr)));
if (user == null)
{
return NotFound();
}
else
{
return new ObjectResult(user);
}
}
catch (Exception ex)
{
Helpers.ExceptionLogger.LogException(ex);
return StatusCode(500);
}
}
}
//GET: api/users/1
[HttpGet("{id}", Name = "GetUserByID")]
public async Task<IActionResult> GetUserByID(Int32 id)
{
try
{
var user = await _context.Users.FirstOrDefaultAsync(u => u.IDUser == id);
if (user == null)
{
return NotFound();
}
else
{
return new ObjectResult(user);
}
}
catch (Exception ex)
{
Helpers.ExceptionLogger.LogException(ex);
return StatusCode(500);
}
}
#endregion
#region HTTP POST
[HttpPost]
public async Task<IActionResult> CreateUser([FromBody] Models.User userToCreate, string userGroupID)
{
if (userToCreate == null)
{
return BadRequest();
}
else
{
try
{
_context.Users.Add(userToCreate);
int parsingResult;
// if user passed userGroupID
if (userGroupID != null)
{
// parsing if userGroupID is a number
if (!int.TryParse(userGroupID, out parsingResult))
{
return BadRequest();
}
else
{
// if client want to assign a new user to some group
if (parsingResult > 0)
{
// creating new record in UserGroup table - assigning a user to group
var userGroup = new Models.UserGroup();
_context.Entry(userGroup).Property("IDGroup").CurrentValue = parsingResult;
_context.Entry(userGroup).Property("IDUser").CurrentValue = userToCreate.IDUser;
_context.UserGroups.Add(userGroup); // NOTE HERE
}
}
}
await _context.SaveChangesAsync();
return CreatedAtRoute("GetUserByID", new { id = userToCreate.IDUser }, userToCreate);
}
catch (Exception ex)
{
Helpers.ExceptionLogger.LogException(ex);
return StatusCode(500);
}
}
}
#endregion
}
User model :
public class User
{
[Key]
public int IDUser { get; set; }
[Required]
public string Name { get; set; }
public List<UserGroup> UsersGroups { get; set; }
}
UserGroup model :
public class UserGroup
{
public Group Group { get; set; }
public User User { get; set; }
}
DBContext class :
public class DBContext : DbContext
{
public DBContext(DbContextOptions<DBContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// shadow property - foreign key
modelBuilder.Entity<UserGroup>()
.Property<int>("IDUser");
// shadow property - foreign key
modelBuilder.Entity<UserGroup>()
.Property<int>("IDGroup");
modelBuilder.Entity<UserGroup>()
.HasKey( new string[]{ "IDUser", "IDGroup" });
modelBuilder.Entity<UserGroup>()
.HasOne(ug => ug.Group)
.WithMany(g => g.UsersGroups)
.HasForeignKey("IDGroup");
modelBuilder.Entity<UserGroup>()
.HasOne(ug => ug.User)
.WithMany(u => u.UsersGroups)
.HasForeignKey("IDUser");
base.OnModelCreating(modelBuilder);
}
public DbSet<Group> Groups { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<UserGroup> UserGroups { get; set; }
}
The problem lies in HttpPost method in UsersController.
When I do "normal" POST and pass JSON object which contain a user to add without assigning it to group (empty userGroupID parameter) everything is ok - user gets added to the DataBase and Postman returns me a user with its ID.
screen :
and when I try to add a new user but with adding it to specific group I always get an error :
screen :
Even despite that error new user gets properly added to DB and associated with its group (record gets added to UserGroup table; UserGroup is join table between Users and Groups table). So I have proper data in my DB but I always get this error and I can't return new added user to client who called API and can't get his ID. Am I doing something wrong in my CreateUser method ?
UPDATE :
I have added a comment line in "NOTE HERE" in CreateUser method in UsersController. If I comment whole this line I don't get an error from Postman but obviously I don't get my User associated with its group (I don't get new record added to UserGroup join table). So it seems like another Add method on context object causing an error ... Does it make sense ?
Did you try to debug it?
Set a breakpoint on the row:
if (userToCreate == null)
Send again the request with Postman and debug your app. There you can see what and where it goes wrong.
Please let me know how it is going so I know how can I help you:)

Returning ObjectID as a string from ASP.NET Core

How to you get a string representation of the ObjectId returned via ASP.NET Core.
I have the following result of an action in my controller:
return new ObjectResult(new { session, user });
One of the user properties is the UserId that is of the ObjectId type.
However, this gets returned in the response as
"id": {
"timestamp": 1482840000,
"machine": 6645569,
"pid": 19448,
"increment": 5052063,
"creationTime": "2016-12-27T12:00:00Z"
}
I would like the response to simply be 58625d5201c4f202609fc5f3 that is the string representation of the same structure.
Are there any easy way to do this for all returned ObjectIds?
EDIT
Adding some more data
Here are the user class. ObjectId is MongoDB.Bson.ObjectId
public class User
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string PasswordHash { get; set; }
public string PasswordSalt { get; set; }
}
The get method in my controller. Controller is Microsoft.AspNetCore.Mvc.Controller.
[HttpGet("{id}", Name = "GetUser")]
public async Task<IActionResult> Get(ObjectId id)
{
var user = await _repository.GetOne<User>(id);
if (user == null) return NotFound();
return new ObjectResult(user);
}
And this is the method from my repository:
public async Task<T> GetOne<T>(ObjectId id)
{
var collectionname = typeof(T).Name;
var collection = _database.GetCollection<T>(collectionname);
var filter = Builders<T>.Filter.Eq("_id", id);
var result = await collection.Find(filter).ToListAsync();
return result.FirstOrDefault();
}
You have to explicitly write a ObjectID to JSON convertor. Please check the link below:
https://stackoverflow.com/a/37966098/887976

Is it possible to send object as route value in RedirectToRouteResult?

First of all, it needs to create some codes that handle any error in my application like the following code.
public class HandleSomeErrorAttribute : HandleErrorAttribute
{
public string ControllerName { get; set; }
public string ActionName { get; set; }
public override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
if(filterContext.Result != null)
{
var viewResult = filterContext.Result as ViewResult;
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(
new
{
controller = ControllerName,
action = ActionName,
errorInfo = "test"
}
));
}
}
}
Everything works fine if I send test as errorInfo value. In the other hand, if I send some errorInfo object as errorInfo value, action controller will not receive any errorInfo value. It always is null.
I know this behavior is designed to handle any values in form collection that is sent by web browser when user submits form. So, it always read only string value and parses it to object.
So, is it possible to do that?
Thanks,
After I search some related question in Stackoverflow, I just realize that any redirect action in ASP.NET sends HTTP 302 code to browser. After that, browser will create new request to fetch new URL (that can be found in HTTP 302 header).
Therefore, it is impossible to directly send any complex objects (without serialize it) to browser and order browser to send it back to server. Although, it is possible, but I think it is so silly to do that. Because you can use the following code to call another action without send HTTP 302 to browser.
public class TransferResult : ActionResult
{
public TransferResult(string controllerName, string actionName, RouteValueDictionary routeValues = null)
{
RouteValues = routeValues ?? new RouteValueDictionary();
if (!string.IsNullOrEmpty(controllerName))
{
RouteValues[MvcApplication.ControllerRouteKey] = controllerName;
}
if (!string.IsNullOrEmpty(actionName))
{
RouteValues[MvcApplication.ActionRouteKey] = actionName;
}
if(RouteValues[MvcApplication.ControllerRouteKey] == null)
{
throw new ArgumentException(Resources.ControllerNameIsNotFoundInRouteValueDictionary, "controllerName");
}
if (RouteValues[MvcApplication.ActionRouteKey] == null)
{
throw new ArgumentException(Resources.ActionNameIsNotFoundInRouteValueDictionary, "actionName");
}
}
public TransferResult(RouteValueDictionary routeValues)
: this(null, null, routeValues)
{
}
public RouteValueDictionary RouteValues { get; set; }
public override void ExecuteResult(ControllerContext context)
{
var routeData = new RouteData();
foreach (var item in RouteValues)
{
routeData.Values.Add(item.Key, item.Value);
}
var contextWrapper = new HttpContextWrapper(HttpContext.Current);
var request = new RequestContext(contextWrapper, routeData);
var controller = ControllerBuilder.Current.GetControllerFactory().CreateController(context.RequestContext, RouteValues[MvcApplication.ControllerRouteKey].ToString());
controller.Execute(request);
}
}
Next, I create some handle error for handling some error type and transfering it to another action controller.
public class SomeHandleErrorAttribute : HandleErrorAttribute
{
public SomeHandleErrorAttribute (string controllerName, string actionName)
{
ExceptionType = typeof (EntityException);
ControllerName = controllerName;
ActionName = actionName;
}
public string ControllerName { get; set; }
public string ActionName { get; set; }
public override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
if(filterContext.Result != null)
{
var routeValue = new RouteValueDictionary
{
{"errorInfo", filterContext}
};
filterContext.Result = new TransferResult(ControllerName, ActionName, routeValue);
}
}
}
Finally, I create the action for handling this error.
public class ErrorController : Controller
{
public string EntityError(ExceptionContext errorInfo)
{
return string.Format("Handle error for {0} ", errorInfo.Exception.Message);
}
}
Nope. A redirect returns a string to your browser, informing it of the page (and query parameters) it should fetch. You can (and should!) watch this in Fiddler. So whatever you put into a redirect has to be serializable to/from a string.