AngularJS Insert in Web Api is null - asp.net

I'm trying to put together my first CRUD app using AngularJS and Asp.Net Web Api. I have setup the controller with a newMember object:
$scope.newMember = {};
And then call the insert in the factory method as:
dataService.insertMember($scope.newMember);
This calls the method in the dataService:
var _insertMember = function(member) {
return $http.post("/api/clubmembers/", member);
};
which fires the Web Api Post method
public HttpResponseMessage Post([FromBody]PersonViewModel member)
{
//if (_repo.AddClubMember(member) && _repo.Save())
if (_repo.AddClubMember(member) && _repo.Save())
{
return Request.CreateResponse(HttpStatusCode.Created, member);
}
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
I have checked the data in the Angular part of the app and the correct data is passed via $scope.newMember, but when it reaches the Web Api Post method the member parameter is always Null.
If I modify the controller method to the following it passes the data to the Post method as expected:
var testData2 = {
FirstName: $scope.newMember.FirstName,
LastName: $scope.newMember.LastName
};
var a = 1;
dataService.insertMember(testData2);
Is it possible to pass $scope.newMember as the parameter or do I have to fill in the details as of the amended code?
Thanks
Mark

Check your code carefully, I would say (based on your experience described in the question) there will be some typo.
Call with explicit {} - empty object
If you will call your service like this:
dataService.insertMember({}); // just created empty object
you will get instantiated object on your API Post method PersonViewModel member
Call with explicit null or undefined
but in case, that you will call one of these
dataService.insertMember(null);
dataService.insertMember(undefined);
the API Post method will be provided with NULL.
typo (incorrect property name), i.e. undefined
And what that means? that you most likely, somewhere in the call chain used something like this
$scope.newMember ...
...
dataService.insertMember($scope.newMemberXXX);
where newMemberXXX represents any undefined property, resulting in undefined to be passed

If you add FirstName and LastName properties to newMember it should work fine:
$scope.newMember = {};
$scope.newMember.FirstName = "First Name";
$scope.newMember.LastName = "Last Name";
dataService.insertMember($scope.newMember);
Thing is, $scope.newMember has to match the properties and structure of PersonViewModel for the Web Api to be able to match the two types.
Let us know if this works.

Related

How to Overload HttpPost Web API Method Based Json Datatype Properties

I am asked to implement a REST Web API to a specific route, where either of two different Json Datatypes may be posted.
This results in the following exception being thrown:
Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints.
Is there an Attribute that can be placed on the Web Methods, referencing Properties of the Json payloads so as to disambiguate the two possible Datatypes?
This was covered here but I'll add a little bit.
It's not good API design to do that and goes against Swagger / OpenAPI specifications to do what you're asking.
The only way to do this with the same HTTP method (POST in your case) is to have one action that takes in both models. Check which one isn't null to then route to the correct method to handle that logic.
If you can get away with using a different HTTP verb you could technically do that and have two separate action methods (like POST and PUT), but you wouldn't be using them "correctly" and based on your question and need, I doubt you can do that anyway.
You can read the request body as a string and then try to decide which type to deserialize in:
[HttpPost]
[Route("api/mypath")]
public async Task<IActionResult> MyMethod()
{
request.Body.Position = 0;
var reader = new StreamReader(request.Body, Encoding.UTF8);
var body = await reader.ReadToEndAsync();
if(body.Contains("A))
{
var A = JsonConvert.DeserializeObject<A>(body);
}
else{
var B = JsonConvert.DeserializeObject<B>(body);
}
}
And add a middleware to enable request buffering:
app.Use(next => context => {
context.Request.EnableBuffering();
return next(context);
});
You can read more about it here

Get request not sending parameter via url to the api controller

I have this simple API controller in a NetCore 2.2 web game that is supposed to return a list of monsters based on the dungeonID(Guid).
So I use the URL, passing in the guid of the dungeonID, to that controller like this:
https://localhost:44361/MonsterList/GetMonsters/2f14c8gf-2e7e-466a-bcbg-f4440e92b3dg
But when I step through the code, I just see all zeroes for the dungeonID:
public async Task<JsonResult> GetMonsters(Guid dungeonID)
{
var monsters = await _context.MonsterList.Where(c => c.DungeonID == (dungeonID)).ToListAsync();
return Json(monsters);
}
This returns nothing because, for reasons I don't know, dungeonID is always all zeroes.
But this does work if I hard-code in the dungeonID:
https://localhost:44361/MonsterList/GetMonsters
public async Task<JsonResult> GetMonsters()
{
var monsters = await _context.MonsterList.Where(c => c.DungeonID == Guid.Parse("2f14c8gf-2e7e-466a-bcbg-f4440e92b3dg")).ToListAsync();
return Json(monsters);
}
I've seen lots of posts similiar to mine, like these:
asp.net webapi 2 post parameter is always null
Post parameter is always null
But nothing seems to work.
How do I add the ability to pass in a Guid parameter?
Thanks!
One point is 2f14c8gf-2e7e-466a-bcbg-f4440e92b3dg is not a GUID/UUID . Just try with a correct GUID :
https://localhost:44384/api/values/GetMonsters/2e6ae748-10c2-4e23-84c3-9d3db7c09631

How to send templated emails?

How does one send a templated Postmark message on ASP.NET? I'd imagine it would be done in this way:
TemplatedPostmarkMessage message = new TemplatedPostmarkMessage
{
From = "demo#demo.com",
To = "someone#else.com",
TemplateId = 1738,
TemplateModel = some_passed_in_model
};
The question now arises, what exactly is TemplateModel? From the API on Postmark's site, it seems like a JSON object, but in the definition from the DLL, it's as follows:
public object TemplateModel { get; set; }
I tried creating my own object with variable names that correspond to those on the Postmark template, however that does not work (it just sends a blank template). Postmark also does not have any documentation on how to use TemplatedPostmarkMessage in ASP.NET yet.
We send a dictionary of <string, object>, I believe you can use more complex models but a dictionary will get the job done.

Ajax-Enabled WCF service : understanding Javascript parameters when calling a function

I am toying with Ajax-Enabled WCF services. So far I've managed to create and consume my service with a small client application. However while reading the "How-to" page on the msdn website, I've came accross this piece of code :
function Button1_onclick() {
var service = new SandwichServices.CostService();
service.CostOfSandwiches(3, onSuccess, null, null);
}
function onSuccess(result) {
alert(result);
}
and I fail to understand (or even find information about) the null, null parameters when calling the function. 3 is the parameter you want to pass to the service function, onSuccess is the function that is called on a successfull callback but what are those 2 null parameters ?
The last two parameters refer to onFailure and Usercontext respectively for below mentioned question.
service.CostOfSandwiches(3, onSuccess, null, null);
OnFailure : As the name suggest, if ajax call fails because of network issue, server error, timeout etc.., function passed here as argument will invoked.
UserContext: Generally all ajax call (I mean this[WCF Ajax] is too dependent on ajax call) uses this parameter. Because, there is a main method that call $.ajax, in your case it is Button1_onclick. So upto this point all variables defined will not be available in callbacks ( success or failure), because these are called back when ajax call completes, but to make those call aware of previously defined variable, context option is used to pass them.
Example:
function Button1_onclick() {
var service = new SandwichServices.CostService();
var some_var = "someval"; // on successcallback if you are trying to access this, it will show error.
service.CostOfSandwiches(3, onSuccess, null, null);
}
To use this in success callback , you have to pass that as usercontext object -- like this service.CostOfSandwiches(3, onSuccess, null, some_var);
OnSuccessMethod -
function onSuccess(result,usctx,methodname)
{
if (usctx == "someval")
{
alert(result);
}
}
For more info on context option in general ajax sense , refer this - How to pass context in jquery ajax success callback function
They are callbacks for sucess, error, and userContext. You can read a bit more here under the "Accessing WCF web services" section.

How do QueryString parameters get bound to Action method parameters?

I have a webforms project, and am attempting to run some code that allows me to make a call to an MVC route and then render the result within the body of the web forms page.
There are a couple of HttpResponse/Request/Context wrappers which I use to execute a call to an MVC route, e.g.:
private static string RenderInternal(string path)
{
var responseWriter = new StringWriter();
var mvcResponse = new MvcPlayerHttpResponseWrapper(responseWriter, PageRenderer.CurrentPageId);
var mvcRequest = new MvcPlayerHttpRequestWrapper(Request, path);
var mvcContext = new MvcPlayerHttpContextWrapper(Context, mvcResponse, mvcRequest);
lock (HttpContext.Current)
{
new MvcHttpHandlerWrapper().PublicProcessRequest(mvcContext);
}
...
The code works fine for executing simple MVC routes, for e.g. "/Home/Index". But I can't specify any query string parameters (e.g. "/Home/Index?foo=bar") as they simply get ignored. I have tried to set the QueryString directly within the RequestWrapper instance, like so:
public class MvcPlayerHttpRequestWrapper : HttpRequestWrapper
{
private readonly string _path;
private readonly NameValueCollection query = new NameValueCollection();
public MvcPlayerHttpRequestWrapper(HttpRequest httpRequest, string path)
: base(httpRequest)
{
var parts = path.Split('?');
if (parts.Length > 1)
{
query = ExtractQueryString(parts[1]);
}
_path = parts[0];
}
public override string Path
{
get
{
return _path;
}
}
public override NameValueCollection QueryString
{
get
{
return query;
}
}
...
When debugging I can see the correct values are in the "request.QueryString", but the values never get bound to the method parameter.
Does anyone know how QueryString values are used and bound from an http request to an MVC controller action?
It seems like the handling of the QueryString value is more complex than I anticipated. I have a limited knowledge of the internals of the MVC Request pipeline.
I have been trying to research the internals myself and will continue to do so. If I find anything I will update this post appropriately.
I have also created a very simple web forms project containing only the code needed to produce this problem and have shared it via dropbox: https://www.dropbox.com/s/vi6erzw24813zq1/StackMvcGetQuestion.zip
The project simply contains one Default.aspx page, a Controller, and the MvcWrapper class used to render out the result of an MVC path. If you look at the Default.aspx.cs you will see a route path containing a querystring parameter is passed in, but it never binds against the parameter on the action.
As a quick reference, here are some extracts from that web project.
The controller:
public class HomeController : Controller
{
public ActionResult Index(string foo)
{
return Content(string.Format("<p>foo = {0}</p>", foo));
}
}
The Default.aspx page:
protected void Page_Load(object sender, EventArgs e)
{
string path = "/Home/Index?foo=baz";
divMvcOutput.InnerHtml = MvcWrapper.MvcPlayerFunctions.Render(path);
}
I have been struggling with this for quite a while now, so would appreciate any advice in any form. :)
MVC framework will try to fill the values of the parameters of the action method from the query string (and other available data such as posted form fields, etc.), that part you got right. The part you missed is that it does so by matching the name of the parameter with the value names passed in. So if you have a method MyMethod in Controller MyController with the signature:
public ActionResult MyMethod(string Path)
{
//Some code goes here
}
The query string (or one of the other sources of variables) must contain a variable named "Path" for the framework to be able to detect it. The query string should be /MyController/MyMethod?Path=Baz
Ok. This was a long debugging session :) and this will be a long response, so bear with me :)
First how MVC works. When you call an action method with input parameters, the framework will call a class called "DefaultModelBinder" that will try and provide a value for each basic type (int, long, etc.) and instance of complex types (objects). This model binder will depend on something called the ValueProvider collection to look for variable names in query string, submitted forms, etc. One of the ValueProviders that interests us the most is the QueryStringValueProvider. As you can guess, it gets the variables defined in the query string. Deep inside the framework, this class calls HttpContext.Current to retrieve the values of the query string instead of relying on the ones being passed to it. In your setup this is causing it to see the original request with localhost:xxxx/Default.aspx as the underlying request causing it to see an empty query string. In fact inside the Action method (Bar in your case) you can get the value this.QueryString["variable"] and it will have the right value.
I modified the Player.cs file to use a web client to make a call to an MVC application running in a separate copy of VS and it worked perfectly. So I suggest you run your mvc application separately and call into it and it should work fine.

Resources