Web Api中的Http Post内部服务器错误500& WCF

时间:2014-04-12 06:21:24

标签: c# asp.net wpf asp.net-mvc-4 asp.net-web-api

我使用WPF作为客户端和Web api作为服务。为了在WPF中添加新事件单击按钮和更新按钮而不返回到api控制器中的http post动作并且还返回错误内部服务器错误。 Http Post方法

[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点击事件:

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应用程序中的事件类:

 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

1 个答案:

答案 0 :(得分:0)

您已将URI的某些部分包含两次。

   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;

你复制了#34; api / Event"。并获得URI,api / Event / api / Event /。对于您的BaseAddress,您只需要主机名和端口。

e.g。

   client.BaseAddress = new Uri("http://localhost:40926");
   var response = client.PostAsJsonAsync("api/Event", event1).Result;

此外,您从服务器代码中的其他位置收到500内部服务器错误。我现在无法到达目的地。