创建webAPI的老手在哪里

时间:2019-05-13 13:32:35

标签: api model-view-controller model controller frameworks

在一个新的Web应用程序中,我将尝试使用实体框架使用读/写操作创建一个新的Web API控制器。

安装了最新版本的Visual Studio。

问题是我没有成功重新安装Visual Studio 2015

旧方式

// PUT api/customers/5 Post changed to put 
public HttpResponseMessage Puteddict_customer(int id, eddict_customer eddict_customer) {
    if (!ModelState.IsValid) {
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
    }

    if (id != eddict_customer.id_customer) {
        return Request.CreateResponse(HttpStatusCode.BadRequest);
    }

    db.eddict_customer.Attach(eddict_customer);
    db.ObjectStateManager.ChangeObjectState(eddict_customer, EntityState.Modified);

    try {
        db.SaveChanges();
    } catch (DbUpdateConcurrencyException ex) {
        return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
    }
    return Request.CreateResponse(HttpStatusCode.OK);
}

// PUT: api/picture_oz1/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> Putpicture_oz(int id, picture_oz picture_oz) {

    if (!ModelState.IsValid) {
        return BadRequest(ModelState);
    }

    if (id != picture_oz.id_picture) {
        return BadRequest();
    }

    db.Entry(picture_oz).State = EntityState.Modified;

    try {
        await db.SaveChangesAsync();
    } catch (DbUpdateConcurrencyException) {
        if (!picture_ozExists(id)) {
                return NotFound();
        } else {
                throw;
        }
    }
    return StatusCode(HttpStatusCode.NoContent);
}

好吧,向导有2个不同的数据库,完全可以充当其他控制器。 我在做什么错了?

输出:当我使用ajax更改数据时:xml解析错误

包括: WebApiConfig.cs

var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

0 个答案:

没有答案
相关问题