-2

I'm trying to save data from WBF inputs to database through ASP.Net Web API following MVVM Pattern. I have checked the function it is also receiving data in proper format, checked by applying breakpoints. This is my Function in WPF:

private async void SubmitExecute ( object parameter )
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:60053/Api/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")
                );
            await client.PostAsJsonAsync("PostSignUp",User);
            WindowsHelper.SignInPage();
        }

I have tried applying breakpoint to my ASP.Net Post Function. It's not getting any hit from my WPF application. But I've checked it through my postman app, the ASP.net function works fine. Code of my ASP.net function:

 [System.Web.Http.HttpPost]
        public void PostSignUp([Bind(Exclude ="Id")]User user)
        {
            try
            {
                user.Id = Guid.NewGuid();
                db.Users.Add(user);
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                ExceptionLogger.LogException(ex);
            }
            
        }
Psychonaut007
  • 167
  • 3
  • 13
  • If your web app is running through IIS, you need to [attach to the running IIS process](https://stackoverflow.com/questions/848987/attach-debugger-to-iis-instance) to debug your server-side API code. Did you look at the database to see if it is saving anything? – Andrew H Oct 04 '21 at 22:29
  • Yes I have checked the database. It hasn't received any data yet – Psychonaut007 Oct 04 '21 at 22:32
  • How are you debugging your controller? How is your web service running? – Andrew H Oct 04 '21 at 22:36
  • I have simply started it by clicking "start". – Psychonaut007 Oct 04 '21 at 22:38
  • I have tried to "attach it to a process" its not working – Psychonaut007 Oct 04 '21 at 22:39
  • 1
    In that case you should definitely be debugging the controller, so I would verify that your Postman request is matching what you are trying to send from code. It's particularly easy to get the port wrong when using localhost, so that's a good place to start. – Andrew H Oct 04 '21 at 22:40
  • Thanks Its working now. – Psychonaut007 Oct 04 '21 at 22:58
  • Consider posting the working solution to your problem as an answer to the question to help others in the future. – Andrew H Oct 04 '21 at 23:02

1 Answers1

-1

I changed the way I was posting data and corrected the URL and it just worked fine.

  private async void SubmitExecute ( object parameter )
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:60053/Api/InventoryAccess/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")
                );
            await client.PostAsJsonAsync("PostSignUp?Name="+User.Name+"&Email="+ User.Email+"&Password="+ User.Name,User);
            WindowsHelper.SignInPage();
        }
Psychonaut007
  • 167
  • 3
  • 13