1

I am sending to c# api. One of the functions isn't sent to the api. can you advise what is the reason. (other functions with this api work well).

component.

    this.loggedUser.aValidateUser(this.newlyRegistered.uMobile).subscribe(res => {
      alert("personal details were saved successfully"),
        this.active = 2;
    }, err => {
      alert("There were wrong details. Please make the necessary changes");
    }
    );
  }

service in angular:

  aValidateUser(phoneNumber:string) {
    return this.HTTP.post('http://localhost:54058/user/ValidateUser', phoneNumber ,this.httpOptions);
  }

c# api:

  [EnableCors("*", "*", "*")]
    [RoutePrefix("user")]
    public class UserController : ApiController
    {
        SenderLogic user = new SenderLogic();

        [HttpPost]
        [Route("UploadFile")]
        public void UploadFile()//Saves passport in local folder
        {
            for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++)
            {


                var file = HttpContext.Current.Request.Files[i];
                if (file != null && file.ContentLength > 0)
                {
                    var fileName = DateTime.Now.ToString("yyyyMMdd_hhmmss") + Path.GetFileName(file.FileName);
                    var path = Path.Combine(HttpContext.Current.Server.MapPath("~/UploadedFiles"), fileName);
                    file.SaveAs(path);
                }
            }
        }

        [Route("ValidateUser")]
        [HttpPost]  
        public bool ValidateUser(string phoneNumber)//check if phone number-(identifier of user) exists
        {      
            if (user.ValidateUser(phoneNumber) == true)
                return true;
            return false;
        }

Debugging showed that the service doesn't send to c# api. while other functions with same api and function type work well.

EDIT function that does work: Angular-

  if (files.length === 0) {
    return;
  }
  let filesToUpload : File[] = files;
  const formData = new FormData();
  Array.from(filesToUpload).map((file, index) => {
    return formData.append('file'+index, file, file.name);
  });
  this.clientHttp.post('http://localhost:54058/user/UploadFile', formData, {reportProgress: true, observe: 'events'})
    .subscribe(event => {
      if (event.type === HttpEventType.UploadProgress)
        this.progress = Math.round(100 * event.loaded / event.total);
      else if (event.type === HttpEventType.Response) {
        this.message = 'Upload success.';
      }
    });
}

c# api:

 [HttpPost]
        [Route("UploadFile")]
        public void UploadFile()//Saves passport in local folder
        {
            for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++)
            {

                var file = HttpContext.Current.Request.Files[i];
                if (file != null && file.ContentLength > 0)
                {
                    var fileName = DateTime.Now.ToString("yyyyMMdd_hhmmss") + 
                  Path.GetFileName(file.FileName);
                    var path = 
       Path.Combine(HttpContext.Current.Server.MapPath("~/UploadedFiles"), 
      fileName);
                    file.SaveAs(path);
                }
            }
        }
4
  • is your proxy setup from angular to .net? angular runs on 4200 by default Commented Nov 24, 2019 at 19:22
  • @d.moncada it is a call to c# api Commented Nov 24, 2019 at 19:23
  • If other functions work well maybe you should look at them and see difference? There must be some. You can also add an example of such working functions. And what does it mean it "doesn't send"? In browser do you see request going out but it does not reach server? Commented Nov 24, 2019 at 19:28
  • 1
    @GrayCat see edit. Getting 404 error when sending to server. Commented Nov 24, 2019 at 19:43

1 Answer 1

2

Two things that I hope will help you.

  1. Try to change signature of

    public bool ValidateUser(string phoneNumber)
    

    to

    public bool ValidateUser([FromBody] string phoneNumber)
    
  2. In angular change:

    return this.HTTP.post('http://localhost:54058/user/ValidateUser', phoneNumber ,this.httpOptions);
    

    to

    return this.HTTP.post('http://localhost:54058/user/ValidateUser', JSON.stringify(phoneNumber) ,this.httpOptions);
    

In general avoid passing primitive types (int, string etc) to POST methods. Wrap them in a model class for example

public class UserPhone {
   public string Phone {get; set;}
}

The controller method should be changed like this:

public bool ValidateUser([FromBody] UserPhone phoneNumber)

and angular method to:

return this.HTTP.post('http://localhost:54058/user/ValidateUser', {phone: phoneNumber} ,this.httpOptions);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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