WebApi Mediatypeformatter错误

时间:2017-08-26 11:55:16

标签: c# asp.net-web-api

我收到错误,我无法弄清楚如何解决这个问题。有人可以帮忙吗?

  

"消息":"请求实体的媒体类型' multipart / form-data'是   此资源不支持。",
  " ExceptionMessage":"没有   MediaTypeFormatter可用于读取类型为' CrudModel'的对象。   来自媒体类型的内容' multipart / form-data'。",
  " ExceptionType":" System.Net.Http.UnsupportedMediaTypeException",
  " StackTrace":"在   System.Net.Http.HttpContentExtensions.ReadAsAsync [T](HttpContent   内容,类型类型,IEnumerable 1格式化程序,IFormatterLogger   formatterLogger,CancellationToken cancellationToken)\ r \ n at   System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage   请求,类型类型,IEnumerable' 1格式化程序,IFormatterLogger   formatterLogger,CancellationToken cancellationToken)"

WebApiConfig.cs

    config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
 );

 EnableCorsAttribute cors = new EnableCorsAttribute("*", "*", "*");
 config.EnableCors(cors);

在我的controller.cs上,我有以下代码:

[HttpPost]
[Route("InsertCrudApp")]
public HttpResponseMessage InsertValuesCurd([FromBody]CrudModel Insertvalues)
{
    CrudModel objCrudModel = new CrudModel();
    objCrudModel.CrudInsertApp();
    return Request.CreateResponse(HttpStatusCode.OK);
}

在我的Model.cs上,我有以下代码:

public bool CrudInsertApp()
{
    SqlConnection con = new SqlConnection(GetSqlConnection());
    SqlCommand cmd = new SqlCommand("usp_insert_CrudApp", con);
    con.Open();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@FirstName", this.FirstName.ToString());
    cmd.Parameters.AddWithValue("@LastName", this.LastName.ToString());
    cmd.Parameters.AddWithValue("@Position", this.Position.ToString());
    cmd.Parameters.AddWithValue("@Status", this.Status);
    cmd.Parameters.AddWithValue("@Company", this.Company.ToString());
    int insertResult = cmd.ExecuteNonQuery();
    con.Close();
    if (insertResult > 0)
        return true;
    else
        return false;
 }

0 个答案:

没有答案