I have a URL that looks like this:
example.com/users/34/
I want for Angular to be able to take this ID (34) and make a call to my ASP.net server to grab information about that user.
As per Angular's documentation (https://docs.angularjs.org/api/ng/service/$http#get) I am thinking I should be able to do something like this in order to achieve that:
$http.get("/api/users/", { UserID: 34 } ).
success(function (user, status, headers, config) {
console.log(user);
}).
error(function (data, status, headers, config) {
console.log(status);
});
But I cannot manage to write an ASP.net function that is able to grab the UserID
variable. Here's what my C# function looks like at the moment:
[Route("api/users")]
public HttpResponseMessage getUser()
{
// do some stuff... but where is UserID? I can't find it in the header etc
}
When I set up a break point, that function fires successfully, but I cannot get UserID. Looking around and can't seem to find any documentation on this.
Any ideas?
--- Update ---
I've confirmed that this works:
Inside my Angular file:
$http.get("/api/users/?UserID=34" ).
success(function (user, status, headers, config) {
console.log(user);
}).
error(function (data, status, headers, config) {
console.log(status);
});
Inside my C# file:
[Route("api/users")]
public HttpResponseMessage getUser( int UserID )
{
// yay! UserID now equals 34
}
But what I would like to do instead of passing the variable as part of the URL string, is pass it as an Optional configuration object (as per https://docs.angularjs.org/api/ng/service/$http#get).
That's what I am asking help with.
Request.Querystring("UserID")
would be how you get that value in ASPexample.com/users/34/
like url expect a parameter which is calledid
, notUserID
. If you changepublic HttpResponseMessage getUser()
topublic HttpResponseMessage getUser(int UserID)
it might help. But I am not sure if this conflicts with theexample.com/users/34/
routing.public HttpResponseMessage getUser(int id)
in combination with$http.get("/api/users/34" )
.