简体   繁体   English

如何使用 .Net 中的 Web API 从数据库发送 ID 并检索与该 ID 相关的数据

[英]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.

相关问题 如何在 ASP.NET Core Web API 中使用 LINQ 仅从数据库获取最新 ID? - How to get latest id only from database using LINQ in ASP.NET Core Web API? 从特定 Id 的数据库中检索所有数据或事务的 SQL 语句,并在 asp.net 中的图形上绘制相同的图 - SQL statement to retrieve all data or transactions from database of a particular Id and plot same on a graph in asp.net 如何通过ID发送消息到signalR特定的PersistentConnection? (使用asp.net mvc4 web-api) - How to send message to signalR specific PersistentConnection by its ID ? (with asp.net mvc4 web-api) 如何在CRM 2011中检索相关的实体ID - How retrieve related Entity ID in CRM 2011 如何使用.net Web API从员工列表中获取最新的员工ID(字符串) - How can I get latest employee ID (string) from the list of employees using .net web api Get call 如何使用从datagridview检索到的ID将数据保存到数据库? - How to save data to database using retrieved ID from datagridview? 如何从数据库中检索搜索相关数据到listitem? - How to retrieve the search related data from the database to a listitem? 使用 Dapper 从数据库中检索用户 ID 失败 - Fail to retrieve the User Id from Database using Dapper 如何使用 EF 内核在 Asp.net 内核 web API 中保存相关模型中的数据? - How to save Data in Related Models in Asp.net core web API using EF core? 如何使用System.Net.Http将access_token和id_token发送到api - How to send an access_token and id_token to an api using System.Net.Http
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM