将数据从MVC发布到WebApi

时间:2018-02-02 05:21:49

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

我正在学习MVC WEB API我在MVC中Controller调用Web Api进行进一步操作。我已成功将数据从API检索到控制器,但我无法将数据从Controller发布到API。 在DAL部分中,我应该在哪里将值放在参数上。我的数据库是Oracle

如何在API中获取数据,因为我仍在学习如果需要对此代码进行任何改进,我们将非常感谢您的建议。

我的MVC控制器

[HttpPost]
public ActionResult Create(Models.DeviceType device)
{
    HttpResponseMessage response; 
    try
    {
        using (var httpClient = new HttpClient())
        {
            httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            response = httpClient.PostAsync(ApiUrl + "DeviceTypeApi/AddDeviceType", device, new JsonMediaTypeFormatter()).Result;
            response.EnsureSuccessStatusCode();
            return View("GetAllDeviceTypes");
        }
    }
    catch (Exception ex)
    {
        return View();
    }
}

API控制器

[HttpPost, ActionName("AddDeviceType")]
public HttpResponseMessage AddDeviceType()
{
    objDeviceBL = new DeviceBL();
    HttpResponseMessage response;
    try
    {
        var deviceResponse = objDeviceBL.AddDeviceType(); //this will further move it to business logic
        if (deviceResponse != null)
        {
            response = Request.CreateResponse<List<Models.DeviceType>>(HttpStatusCode.OK, deviceResponse);
        }
        else
        {
            response = new HttpResponseMessage(HttpStatusCode.NotFound);
        }

    }
    catch (Exception ex)
    {
        response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
    }
    return response;
}

BL

internal new List<Models.DeviceType> AddDeviceType()
{
    return base.AddDeviceType();
}

核心

protected List<DeviceType> AddDeviceType()
{
    List<DeviceType> objDeviceTypes = null;
    try
    {
        objDeviceTypes = new DeviceDAL().AddDeviceType();
    }
    catch (Exception ex)
    {

        throw ex;
    }

    return objDeviceTypes;
}

DAL

public List<DeviceType> AddDeviceType()
{
    List<DeviceType> objDeviceTypes = new List<DeviceType>();
    using (cmd = new OracleCommand("SP_DMS_DEVICE_TYPE_INSERT", con))
    {
        try
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("p_typename", OracleType.VarChar).Direction = ParameterDirection.Input;
            cmd.Parameters.Add("p_createdby", OracleType.Int32).Direction = ParameterDirection.Input;
            cmd.Parameters.Add("p_createdon", OracleType.DateTime).Direction = ParameterDirection.Input;
            cmd.Parameters.Add("p_updatedby", OracleType.Int32).Direction = ParameterDirection.Input;
            cmd.Parameters.Add("p_updatedon", OracleType.DateTime).Direction = ParameterDirection.Input;

            con.Open();
            adap = new OracleDataAdapter();
            adap.InsertCommand = cmd;                    
        }
        catch (Exception)
        {

            con.Close();
        }
        return objDeviceTypes;
    }

}

1 个答案:

答案 0 :(得分:0)

只需更改您的api控制器操作:

[HttpPost, ActionName("AddDeviceType")]
public HttpResponseMessage AddDeviceType(Models.DeviceType device)
{
    //Here You can manipulate with device object
}

ASP会将设备对象从请求json映射到c#对象。