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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23026895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:31:02  来源:igfitidea点击:

Http Post Internal Server Error 500 in Web Api & WCF

c#asp.netwpfasp.net-mvc-4asp.net-web-api

提问by user2226923

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

我使用 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 click event:

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);
            }

} Event class in WPF application:

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; }
}

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

我在 post action 附近使用了断点,还使用了 click 事件。但是在 var response = client.PostAsJsonAsync("api/Event", event1).Result; 附近的点击事件中 不返回 api post 方法并返回 Response 状态代码并不表示成功:500(内部服务器错误)。更新也有类似的问题

-Thanks Sindhu

-谢谢辛杜

回答by Trisk

You included some parts of the URI twice.

您两次包含了 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;

You duplicated "api/Event". And would get the URI, api/Event/api/Event/. For your BaseAddress you only need hostname and port.

您复制了“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;

Also you are getting a 500 Internal Server error from somewhere else in your sever code. I can't where right now.

此外,您从服务器代码的其他地方收到 500 Internal Server 错误。我现在不能在哪里。