Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Net Web API - How to pass a URL as input parameter on a Get

I am trying to pass a URL as a input parameter to a ApiController from an Angular REST call. The URL comes as a query string (this is a Provider Hosted app in SharePoint, I need the URL to query SP FWIW).

Here is the method signature in the ApiController:

// GET: api/ProjectSite/5
public IEnumerable<ProjectSite> Get(string id)
{
    return ProjectSite.GetAllProjectSites(id);
}

And here is where I am making the call in Angular:

var spUrl = "'" + getParameterByName("SPHostUrl") + "'";

var queryUrl = "/api/ProjectSite/" + encodeURIComponent(spUrl);
return $http.get(queryUrl);

This generates a GET request that looks like this:

https://localhost:12345/api/ProjectSite/https%3A%2F%2Fcompany.sharepoint.com%2Fsites%2Fsite_dev%2Fweb

When I do I get 'HTTP 400 (Bad Request)' back. If I stop on a break point in the Angular code and change the input param to a simple string (e.g. 'asdf') the REST call is made and I see the Api is called. If I do not change the string and put a breakpoint inside the Get Api method the breakpoint is not reached, indicating that the code is blowing up somewhere in the route engine.

What I don't get is, while its encoded, the string I am trying to pass in should still be treated as a string, right? I also tried changing the input to Uri but that doesn't appear to work (and Uri isn't listed as a supported input type, anyways).

Anyone know how to pass a URL as input parameter?

like image 659
Lane Goolsby Avatar asked Mar 24 '15 20:03

Lane Goolsby


People also ask

Can we pass object as a parameter to Web API GET method?

Web API provides the necessary action methods for HTTP GET, POST, PUT, and DELETE operations. You would typically pass a single object as a parameter to the PUT and POST action methods. Note that Web API does not support passing multiple POST parameters to Web API controller methods by default.

What is FromBody and FromUri in Web API?

The [FromUri] attribute is prefixed to the parameter to specify that the value should be read from the URI of the request, and the [FromBody] attribute is used to specify that the value should be read from the body of the request.


1 Answers

Have you tried calling it using a query string?

var queryUrl = "/api/ProjectSite?id=" + encodeURIComponent(spUrl);
like image 141
Kirk Liemohn Avatar answered Sep 29 '22 11:09

Kirk Liemohn