Web Api中的Http Post内部服务器错误500& WCF [英] Http Post Internal Server Error 500 in Web Api & WCF

查看:148
本文介绍了Web Api中的Http Post内部服务器错误500& WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用WPF作为客户端和Web api作为服务。用于添加新事件单击按钮和WPF中的更新按钮不返回api控制器中的http post动作并返回错误内部服务器错误。

Http Post方法

Hi
I am using WPF as Client and Web api as Service. For add new event click button and update button in WPF not returning to http post action in api controller and also returning error Internal server error.
Http Post method

[HttpPost]
   public HttpResponseMessage PostEvent(EventFormModel event1)
   {
       if (ModelState.IsValid)
       {
           var command = new CreateOrUpdateEventCommand(event1.EventId, event1.EventAgenda, event1.Description, event1.EventDate, event1.Location, event1.UserId);
           var result = commandBus.Submit(command);
           if (result.Success)
           {
               var response = Request.CreateResponse(HttpStatusCode.Created, event1);
               string uri = Url.Link("DefaultApi", new { id = event1.EventId });
               response.Headers.Location = new Uri(uri);
               return response;
           }
       }
       else
       {
           return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
       }
       throw new HttpResponseException(HttpStatusCode.BadRequest);
   }



WPF点击事件:


WPF click event:

private async void btnNewEvent_Click(object sender, RoutedEventArgs e)
        {
                var event1 = new Event()
                {
                    EventAgenda = txtAgenda.Text,
                    Description = txtEventDescription.Text,
                    Location=txtLocation.Text,
                    UserId=Convert.ToInt32(txtUserId.Text),
                    EventId=Convert.ToInt32(txtEventId.Text),
                    EventDate=Convert.ToDateTime(dateEventDate.Text),
                };
                client.BaseAddress = new Uri("http://localhost:40926/api/Event");
                // Add an Accept header for JSON format.
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
                var response = client.PostAsJsonAsync("api/Event", event1).Result;
                if (response.IsSuccessStatusCode)
                {
                    MessageBox.Show("added" + response);
                    txtAgenda.Text = "";
                    txtEventDescription.Text = "";
                    txtLocation.Text = "";
                    txtUserId.Text = "";
                    txtEventId.Text = "";
                    dateEventDate.Text = "";
                }
                else
                {
                    MessageBox.Show("Error Code" + response.StatusCode + " : Message - " + response.ReasonPhrase);
                }
}



WPF应用程序中的事件类:


Event class in WPF application:

public class Event
   {
       public int EventId { get; set; }
       public int UserId { get; set; }
      // [Required(ErrorMessage = "Agenda Required")]
       //[Display(Name = "Movie Name")]
       public string EventAgenda { get; set; }
     //  [Required(ErrorMessage = "Location Required")]
       public string Location { get; set; }

      // [Required(ErrorMessage = "Date Required")]
       public DateTime EventDate { get; set; }
       public string Description { get; set; }
      // public string ReminderType { get; set; }
   }







我在帖子附近使用了断点动作和点击事件。但是在附近的点击事件中

var response = client.PostAsJsonAsync(api / Event,event1).Result;不退回到api post方法并返回响应状态代码并不表示成功:500(内部服务器错误)。类似的更新问题也是



-Thanks Sindhu




I used breakpoints near post action and also click event. but in click event near
var response = client.PostAsJsonAsync("api/Event", event1).Result; not returing to api post method and returning Response status code does not indicate success: 500 (Internal Server Error). Similar issue for Update also

-Thanks Sindhu

推荐答案

这篇关于Web Api中的Http Post内部服务器错误500& WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆