[英]How to send Id and retrieve data related to that ID from database using Web API in .Net
I want to send CatID as user from client side and in server side I need to accept that and send back response to the client catName,catDes,catPic relevant to entered ID.我想从客户端发送 CatID 作为用户,在服务器端我需要接受它并将响应发送回与输入的 ID 相关的客户端 catName,catDes,catPic。
Below code is used to send Data to client.下面的代码用于向客户端发送数据。 I want to receive req.
我想收到请求。 from client and send back relevant response to client, this needs to done via Web API.
从客户端并将相关响应发送回客户端,这需要通过 Web API 完成。
public class AddcatController : ApiController {公共类 AddcatController : ApiController {
[HttpPost]
public HttpResponseMessage PostCat(Catergory category)
{
using (var db = new NorthwindEntities())
{
db.Categories.Add(new Category()
{
CategoryID = category.catId,
CategoryName = category.catName,
Description = category.catDes,
Picture = category.catPic
});
db.SaveChanges();
}
//return OK;
return Request.CreateResponse(HttpStatusCode.OK);
} }
Maybe something like this, but you would change the ActionResult and return methods to your scenario.也许像这样,但您将更改 ActionResult 并将方法返回到您的场景。
[HttpGet]
//You can change it to HttpResponseMessage
public ActionResult GetCat(int catId)
{
Category resp = null;
using (var db = new NorthwindEntities())
{
resp = db.Categories.FirstOrDefault(f => f.CategoryID == catId);
}
if(resp == null)
{
return NotFound("Category not found"); //You can change it to HttpResponseMessage similar method
}
//return OK;
return Ok(resp);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.