Lane Goolsby
Lane Goolsby

Reputation: 681

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?

Upvotes: 6

Views: 10650

Answers (1)

Kirk Liemohn
Kirk Liemohn

Reputation: 7923

Have you tried calling it using a query string?

var queryUrl = "/api/ProjectSite?id=" + encodeURIComponent(spUrl);

Upvotes: 13

Related Questions