0

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.

11
  • a quick google search and Request.Querystring("UserID") would be how you get that value in ASP Commented Mar 2, 2015 at 18:04
  • @Ronnie For me, Request does not have a method of QueryString. Why would that be? Commented Mar 2, 2015 at 18:05
  • I might be confused, but are you wanting to get the value in ASP? Am I understanding that right? Commented Mar 2, 2015 at 18:06
  • I think you are confused with the default routing and parameter names. By default example.com/users/34/ like url expect a parameter which is called id, not UserID. If you change public HttpResponseMessage getUser() to public HttpResponseMessage getUser(int UserID) it might help. But I am not sure if this conflicts with the example.com/users/34/ routing. Commented Mar 2, 2015 at 18:07
  • 1
    Alternatively you could try: public HttpResponseMessage getUser(int id) in combination with $http.get("/api/users/34" ). Commented Mar 2, 2015 at 18:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.