0

I am calling a web API with angularJS Service.

My webp API method is working fine without parameters '/api/Subpart/'

When i tried to pass parameters , Web API method is not firing.

Angular service

.service('subpartService', ['$http', function ($http) {
     this.getSubparts = function (val) {
       return $http.get('/api/Subpart/'+val); // With parameters not working
     };
 }])

Web API Controller Action

[Route("api/Subpart/{strsubpart}")]
public List<tbl_Employee> GetSubparts(string strsubpart)
{
    Entities db = new Entities();
    var data = db.tbl_Regulation.Where(c => c.Subsection == "ABC");
    return data.ToList();
}
8
  • 1
    What else have you tried. Can you call it with a parameter from your browser? Are you sure that val has a valid non empty/ non null / non undefined string value? Also what does without parameters '/api/Subpart/' mean? This is a route and not a parameter, you cant call the method without it so what do you mean it was working fine without it? Commented Oct 6, 2016 at 18:11
  • Ohh Very Strange. Its working, When i pass my api Url as 'api/Subpart/abc' Commented Oct 6, 2016 at 18:14
  • Is the angular service running on the same web site as where the web api is hosted? If not you will need to provide the complete host address as well. Commented Oct 6, 2016 at 18:15
  • actually my parameter value coming as 'api/Subpart/1301.2(b)' because of 1301.2(b) its not working Commented Oct 6, 2016 at 18:16
  • How to handle this format 1301.2(b) in api url? Commented Oct 6, 2016 at 18:17

2 Answers 2

0

I have the solution.It has pass parameters with ?

this.getSubparts = function (val) {
           var subUrl = "http://localhost/api/Subpart/sub_part?subpart="+ val;
             return $http.get(subUrl);
         };
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, My web APi method is firing When i pass my parameters as localhost/api/Subpart/sub_part?1301.2(b) But instead of 1301.2(b) value coming as 'sub_part' ?
why actual value is not coming in web api method?
0

Try to "escape" the value.

http://www.w3schools.com/jsref/jsref_encodeuri.asp

this.getSubparts = function (val) {
           var subUrl = "http://localhost/api/Subpart/?subpart="+ encodeURI(val);
             return $http.get(subUrl);
         };

Comments

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.