How to Post to WebApi using HttpClient? - asp.net-web-api

I am trying to post to a WebAPI using HttpClient using an authentication token.
However I am always getting default values on the WebAPI method not the actual values I am sending.
This is my code:
C# Console APP:
public static async Task<string> Rent(HttpClient client, string token, int idCommunityAmenity, int idHome, DateTime startDate, DateTime endDate)
{
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost:50634/api/amenity/RentCommunityAmenity");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
var postContent = new
{
idCommunityAmenity = idCommunityAmenity,
idHome = idHome,
startDate = startDate,
endDate = endDate
};
request.Content = new StringContent( JsonConvert.SerializeObject(postContent), Encoding.UTF8, "application/json");
var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
WebAPI
[HttpPost("RentCommunityAmenity")]
public async Task<JsonResult> Post([FromBody]int idCommunityAmenity, [FromBody]int idHome, [FromBody]DateTime startDate, [FromBody]DateTime endDate)
{
var communityAmenity = new AmenityReservation
{
IdCommunityAmenity = idCommunityAmenity,
StartDate = startDate,
EndDate = endDate,
IdHome = idHome
};
_context.AmenityReservation.Add(communityAmenity);
await _context.SaveChangesAsync();
return new JsonResult(true);
}
My guess is that the content is not being set up correctly, because when I inspect it I don't see the the json string.
When I hit the post method I get: idCommunityAmenity = 0, idHome=0,...
Thanks for the help.

create a model for the data you pass to the webapi endpoint.
add all the validation to it.
something like :
[DataContract]
public sealed Class BookingModel
{
[Required]
[DataMember]
public int IdCommunityAmenity { get; set; }
[DataMember]
public DateTime StartDate { get;set;}
[DataMember]
public DateTime EndDate { get; set; }
[Required]
[DataMember]
public int IdHome { get; set;}
}
Use whatever other validation you need on the model. DataContract and DataMember comes from System.ComponentModel.DataAnnotations which you add as a reference separately. Sometimes, depending on how your project is setup, your api will not receive data from your post because the property members don't serialize. Making sure you have those can actually help a lot.
Now in webapi you can check your model is valid like this:
[HttpPost("RentCommunityAmenity")]
public async Task<JsonResult> Post([FromBody] BookingModel)
{
if ( !ModelState.IsValid )
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
//your code here.
}

This is the way I fix it.
I took the reference from this answer
Basically you have to receive an object on the WebAPI side.
Like this:
[HttpPost("RentCommunityAmenity")]
public JsonResult Post([FromBody]MyModel value)
{
}
public class MyModel
{
public int idCommunityAmenity { get; set; }
public int idHome { get; set; }
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
}

Related

Creating an object with other object properties from DbContext in MVC EF Core

I am sorry if the answer to this question is so obvious, but all (and I really mean all) of my Google search results are purple, regardless of the search phrase I try to wrap around my question.
I am trying to build an MVC .NET Core 3.0 application with Code-First.
I am able to create my models, have these setup correct (I think) in my Database (Azure SQL), and using Visual Studio's standard templates to create controllers and views. I am therefore able to create each one of my model individually.
What I am trying to do is
Create a view from where I can create a Rental-object, with link to RentalOwner and ParkingSpot.
(Create a view from where I can see all rentals created. This is not in scope for this question)
My models
public class ParkingSpot
{
public int ParkingSpotId { get; set; }
public int ParkingSpotNumber { get; set; }
}
public class RentalOwner
{
public int RentalOwnerId { get; set; }
public int TenantId { get; set; }
public Tenant Tenant { get; set; }
}
public class Tenant
{
public int TenantId { get; set; }
[Required]
[Display(Name="Navn")]
public string Name { get; set; }
[EmailAddress]
[Required]
public string Email { get; set; }
}
public class Rental
{
public int RentalId { get; set; }
public int ParkingSpotId { get; set; }
public ParkingSpot ParkingSpot { get; set; }
public int RentalOwnerId { get; set; }
public RentalOwner RentalOwner { get; set; }
}
I have tried creating a ViewModel, to use for creation of the Rental-model and the binding to other models.
public class RentalCreationView
{
public int Id { get; set; }
public Rental Rental { get; set; }
public int ParkingSpotId { get; set; }
public int TenantId { get; set; }
}
I've tried with this GET and HttpPost POST Actions in my Controller.
// GET: Rentals/Create
public async Task<IActionResult> CreateAsync()
{
var parkingSpots = await _context.ParkingSpots.ToListAsync();
ViewData["ParkingSpots"] = new SelectList(parkingSpots, "ParkingSpotId", "ParkingSpotNumber");
var tenants = await _context.Tenants.ToListAsync();
ViewData["Tenants"] = new SelectList(tenants, "TenantId", "Name");
return View();
}
public async Task<IActionResult> Create(RentalCreationView rcw)
{
if (ModelState.IsValid)
{
_context.RentalOwners.Add(rcw.Rental.RentalOwner);
_context.Rentals.Add(rcw.Rental);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(rcw);
}
I don't think my issue is with my View, and I guess I am missing something in either my models or in my controller.
Right now, when I try to create a Rental, the view is just loading for a couple of minutes after submit.
I have seen numerous of tutorials, read hundreds of questions and articles, and I know that I am missing some basic steps, but the code above is what I have right now (I have about 500 lines of commented out code that doesn't work).
I've spent 2 weeks trying to learn how to do this but I need some help from someone who knows what I'm trying to do.
Any relevant links, videos or documentation would be awesome.. I am really stuck deep.
Thank you
EDIT: I have found a solution, and to all future readers who got this page from their 1000th Google search, what I did was change a bit in the models and add this HttpPost Create in my Controller.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(RentalCreationView rcw)
{
List<ParkingSpot> parkingSpots = _context.ParkingSpots.ToList();
List<Tenant> tenants = _context.Tenants.ToList();
RentalOwner ro = new RentalOwner
{
Tenant = tenants.Find(t => t.TenantId == rcw.TenantId),
OwnerSince = rcw.StartDate
};
Rental ren = new Rental
{
StartDate = rcw.StartDate,
ParkingSpot = parkingSpots.Find(ps => ps.ParkingSpotId == rcw.ParkingSpotId),
RentalOwner = ro
};
if (ModelState.IsValid)
{
_context.RentalOwners.Add(ro);
_context.Rentals.Add(ren);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
else
{
var errors = ModelState.Select(x => x.Value.Errors)
.Where(y => y.Count > 0)
.ToList();
}
return View();
}

HttpClient Xamarin.Forms

I have an application that allows users to log in via facebook. I am trying to save each user to my database using my WebApi. However, I am encountering this exception error: System.NullReferenceException: Object reference not set to an instance of an object. Can anyone see what I am doing incorrectly to cause this. Thanks.
CustomerService class:
public async Task<int> AddCustomer(Customer cust)
{
var data = JsonConvert.SerializeObject(cust);
var content = new StringContent(data, Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.Add("X-Giftworx-App", "Posworx");
var response = await client.PostAsync("http/my api address/api/Customer/Insert", content);
var result = JsonConvert.DeserializeObject<int>(response.Content.ReadAsStringAsync().Result);
return result;
}
Customer class:
public class Customer
{
public string Token { get; set; }
public bool Authenticated { get; set; }
public string SecretKey { get; set; }
public int StoreCustomerID { get; set; }
public string Number { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public object Address { get; set; }
public string Email { get; set; }
public string City { get; set; }
public string Region { get; set; }
public string Country { get; set; }
public string MobilePhone { get; set; }
public DateTime DOB { get; set; }
public object Phone { get; set; }
public object DeviceToken { get; set; }
public object Details { get; set; }
public object Gender { get; set; }
public bool IsError { get; set; }
public object ErrorMessage { get; set; }
public bool PhoneVerified { get; set; }
}
FacebookRender
public class FacebookRender : PageRenderer
{
CustomerService customerService;
public FacebookRender()
{
var activity = this.Context as Activity;
var auth = new OAuth2Authenticator(
clientId: "my app client's id",
scope: "",
authorizeUrl: new Uri("https://www.facebook.com/dialog/oauth/"),
redirectUrl: new Uri("https://www.facebook.com/connect/login_success.html")
);
auth.Completed += async (sender, eventArgs) =>
{
if (eventArgs.IsAuthenticated)
{
await AccountStore.Create().SaveAsync(eventArgs.Account, "FacebookProviderKey");
var accessToken = eventArgs.Account.Properties["access_token"].ToString();
var expiresIn = Convert.ToDouble(eventArgs.Account.Properties["expires_in"]);
var expiryDate = DateTime.Now + TimeSpan.FromSeconds(expiresIn);
var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/me"), null, eventArgs.Account);
var response = await request.GetResponseAsync();
var obj = JObject.Parse(response.GetResponseText());
var id = obj["id"].ToString().Replace("\"", "");
var name = obj["name"].ToString().Replace("\"", "");
Customer cust = new Customer();
cust.Token = accessToken;
cust.Name = name;
await customerService.AddCustomer(cust);
App.NavigateToProfile(string.Format(name));
}
else
{
App.NavigateToProfile("Invalid Login");
}
};
activity.StartActivity(auth.GetUI(activity));
}
}

the correct way to define web api post method

I have a .NET Web Api REST server which has a controller for the class customer which has a post method like this:
public HttpResponseMessage PostCustomer()
{
//getting the data in the request body****
return new HttpResponseMessage(HttpStatusCode.Created);
}
and this is my class:
class Customer
{
public Customer(string name, string tell, string pass, string add)
{
FnameLname = name;
Address = add;
Password = pass;
AccountNumber = tell;
}
public int CustomerId { get; set; }
public string AccountNumber { get; set; }
public string Password { get; set; }
public string Address { get; set; }
public string FnameLname { get; set; }
}
and i have a C# form application consumer which i'm using RESTSharp to do so.
i'm trying to do a post request to create a Customer but i had no luck setting it up.
this is what i have got so far:
{
Customer newc=new Customer(...);
var client = new RestClient("http://192.168.137.1:9090/");
var request = new RestRequest("api/Customer",Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddObject(newc);
var response = client.Execute(request);
}
but it doesn't work.
i don't know if my controller method is wrong or the request is wrong.
Your Post method needs to take in a Customer parameter so that the WebAPI will try to bind the JSON in the request body to the customer parameter
public HttpResponseMessage PostCustomer(Customer customer)
{ ... }

Having issue while trying to pass two model to the same view at a time in mvc 3

I'm trying to create my profile type page for my simple blog site. I have two simple model class like this:
public class UserInfoModel
{
public string UserName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
}
public class NewPost
{
public string PostTitle { get; set; }
public string PostStory { get; set; }
}
I have created a joint model class of user & post to pass to view like this:
public class UserPostModel
{
public UserInfoModel User { get; set; }
public NewPost Post { get; set; }
}
The methods I wrote to retrieve the user & post info are like this:
public int GetUserID(string _UserName)
{
using (var context = new TourBlogEntities1())
{
var UserID = from s in context.UserInfoes
where s.UserName == _UserName
select s.UserID;
return UserID.Single();
}
}
public UserInfo GetUserDetails(int _UserID)
{
using (var context = new TourBlogEntities1())
{
var UserDetails = (from s in context.UserInfoes
where s.UserID == _UserID
select s).Single();
return UserDetails;
}
}
public Post GetUserPosts(int _UserID)
{
using (var context = new TourBlogEntities1())
{
var entity = (from s in context.Posts
where s.UserID == _UserID
select s).Single();
return entity;
}
}
And finally I'm calling all my method from my controller action like this:
[Authorize]
public ActionResult MyProfile()
{
var Business = new Business();
var UserID=Business.GetUserID(User.Identity.Name);
var UserEntity=Business.GetUserDetails(UserID);
var PostEntity=Business.GetUserPosts(UserID);
var model = new UserPostModel();
model.User.UserName = UserEntity.UserName; // problem showing here
model.User.Email = UserEntity.Email;
model.Post.PostTitle = PostEntity.PostTitle;
model.Post.PostStory = PostEntity.PostStory;
return View("MyProfile",model);
}
A run time error showing like " object is not referenced to a object type or null object". I worked ok in a very similar way while passing single model. Whats I'm doing wrong here?
Modified your UserPostModel
public class UserPostModel
{
public UserPostModel()
{
User = new UserInfoModel();
Post = new Post();
}
public UserInfoModel User { get; set; }
public NewPost Post { get; set; }
}
NOTE: check each value before set to model it should not be null.

Sending model data from Knockout back to MVC 3

I am new to knockout and am having a hard time trying to get my data from Knockout back to my server. I keep getting an error of 'No parameterless constructor defined for this object.' Any help would be appreciated.
My Knockout model is as follows
function partSummary(item) {
var self = this;
self.ID = ko.observable(item.ID);
self.serialNumber = ko.observable(item.SerialNumber);
self.description = ko.observable(item.Description);
self.manufacturer = ko.observable(item.Manufacturer);
self.creationDate = ko.observable(item.DateCreated);
self.active = ko.observable(item.IsActive);
}
My code to send the data back the server is
self.savePart = function() {
$.ajax("/PartKO/UpdatePart", {
data: ko.toJSON(self.partDetails),
type: "post",
contentType: 'application/json',
dataType: 'json'
});
};
My MVC controller is
[HttpPost]
public JsonResult UpdatePart(PartDetail part)
{
var dbPart = new PartGenericAccessor();
dbPart.ID = part.ID;
dbPart.Load();
dbPart.Description = part.Description;
dbPart.IsActive = Convert.ToBoolean(part.IsActive);
var manufacturers = ManufacturerAccessor.LoadAll<ManufacturerAccessor>();
if (part.Manufacturer != null)
{
var manufacturer = (from p in manufacturers where p.Name == part.Manufacturer select p.ID).First();
dbPart.ManufacturerID = manufacturer;
}
dbPart.Save();
return Json("Success!!");
}
And my server side model is
public class PartDetail
{
public PartDetail(Guid id, string serial, string description, string manufacturer, DateTime created, bool isActive)
{
ID = id;
SerialNumber = serial;
Description = description;
Manufacturer = manufacturer;
DateCreated = created.ToShortDateString();
IsActive = isActive.ToString(CultureInfo.InvariantCulture);
}
public Guid ID { get; set; }
public string SerialNumber { get; set; }
public string Description { get; set; }
public string Manufacturer { get; set; }
public string DateCreated { get; set; }
public string IsActive { get; set; }
}
You need to supply a parameterless constructor for your MVC model:
public class PartDetail
{
public PartDetail()
{ ... }
}
When the data comes back from the server, MVC will create an empty object, using the parameterless constructor and then call the 'set' methods to set each property that matches the data coming in.
Once I made stupid mistake:
Named controller argument 'action' - and on post it allways null.
[HttpPost]
public JsonResult AddMetaAction(ActionModel action)
I didn't know about that and spend on debug and solving that problem about a half of hour:(

Resources