Asp.net web api 如何在ASP.NETWebAPI中使用多个输入参数并通过存储过程从数据库中获取数据
我在ASP.NET Web API应用程序中使用了多个输入参数,但没有得到输出 我的代码在这里:Asp.net web api 如何在ASP.NETWebAPI中使用多个输入参数并通过存储过程从数据库中获取数据,asp.net-web-api,Asp.net Web Api,我在ASP.NET Web API应用程序中使用了多个输入参数,但没有得到输出 我的代码在这里: [HttpGet] [Route("api/tblProducts/{Price1}/{Price2}/{CategoryId}/{Color}/{Size}")] [ResponseType(typeof(IEnumerable<GetSimilarProducts_Result>))] public IHttpActionResult GetSimlarProduct(Decimal
[HttpGet]
[Route("api/tblProducts/{Price1}/{Price2}/{CategoryId}/{Color}/{Size}")]
[ResponseType(typeof(IEnumerable<GetSimilarProducts_Result>))]
public IHttpActionResult GetSimlarProduct(Decimal Price1,Decimal Price2,string CategoryId, string Color, string Size)
{
IEnumerable<GetSimilarProducts_Result> tblSmlrProduct = db.GetSimilarProducts(Price1, Price2,CategoryId, Color, Size ).AsEnumerable();
if (tblSmlrProduct == null)
{
return NotFound();
}
return Ok(tblSmlrProduct);
}
请帮助我如何从数据库和我的其他存储过程中获取数据
我的方法代码由visual studio生成
public virtual ObjectResult<GetSimilarProducts_Result> GetSimilarProducts(Nullable<decimal> price1, Nullable<decimal> price2, string size, string categoryId, string color)
{
var price1Parameter = price1.HasValue ?
new ObjectParameter("Price1", price1) :
new ObjectParameter("Price1", typeof(decimal));
var price2Parameter = price2.HasValue ?
new ObjectParameter("Price2", price2) :
new ObjectParameter("Price2", typeof(decimal));
var sizeParameter = size != null ?
new ObjectParameter("Size", size) :
new ObjectParameter("Size", typeof(string));
var categoryIdParameter = categoryId != null ?
new ObjectParameter("CategoryId", categoryId) :
new ObjectParameter("CategoryId", typeof(string));
var colorParameter = color != null ?
new ObjectParameter("Color", color) :
new ObjectParameter("Color", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<GetSimilarProducts_Result>("GetSimilarProducts", price1Parameter, price2Parameter, sizeParameter, categoryIdParameter, colorParameter);
}
public virtual ObjectResult<GetSimilarProducts_Result> GetSimilarProducts(Nullable<decimal> price1, Nullable<decimal> price2, string size, string categoryId, string color)
{
var price1Parameter = price1.HasValue ?
new ObjectParameter("Price1", price1) :
new ObjectParameter("Price1", typeof(decimal));
var price2Parameter = price2.HasValue ?
new ObjectParameter("Price2", price2) :
new ObjectParameter("Price2", typeof(decimal));
var sizeParameter = size != null ?
new ObjectParameter("Size", size) :
new ObjectParameter("Size", typeof(string));
var categoryIdParameter = categoryId != null ?
new ObjectParameter("CategoryId", categoryId) :
new ObjectParameter("CategoryId", typeof(string));
var colorParameter = color != null ?
new ObjectParameter("Color", color) :
new ObjectParameter("Color", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<GetSimilarProducts_Result>("GetSimilarProducts", price1Parameter, price2Parameter, sizeParameter, categoryIdParameter, colorParameter);
}
更新:我的存储过程运行良好
USE [MakaAnOrderDB]
GO
/****** Object: StoredProcedure [dbo].[SingleProductDetails] Script Date: 12/14/2017 12:44:24 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Procedure [dbo].[SingleProductDetails]
@ProductId nvarchar(255)=''
As
Begin
Select* from tblProduct where ProductId=@ProductId
End
GO
更新:这是从SingleProductDetails过程获取信息的c代码
[HttpGet]
[Route("api/tblProducts/{productId}")]
[ResponseType(typeof(IEnumerable<SingleProductDetails_Result>))]
public IHttpActionResult Get(string productId)
{
IEnumerable<SingleProductDetails_Result> tblProduct = db.SingleProductDetails(productId).AsEnumerable();
if (tblProduct == null)
{
return NotFound();
}
return Ok(tblProduct);
}
[HttpGet]
[路由(“api/tblProducts/{productId}”)]
[责任类型(类型(IEnumerable))]
公共IHttpActionResult获取(字符串productId)
{
IEnumerable TBLPProduct=db.SingleProductDetails(productId).AsEnumerable();
if(tblProduct==null)
{
返回NotFound();
}
返回Ok(TBL产品);
}
更新:VisualStudio为过程sigleProductDetails编写的代码
public virtual ObjectResult<SingleProductDetails_Result> SingleProductDetails(string productId)
{
var productIdParameter = productId != null ?
new ObjectParameter("ProductId", productId) :
new ObjectParameter("ProductId", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<SingleProductDetails_Result>("SingleProductDetails", productIdParameter);
}
公共虚拟对象结果SingleProductDetails(字符串productId)
{
var productIdParameter=productId!=null?
新的ObjectParameter(“ProductId”,ProductId):
新的ObjectParameter(“ProductId”,typeof(string));
返回((IObjectContextAdapter)this.ObjectContext.ExecuteFunction(“SingleProductDetails”,productIdParameter);
}
您应该遵循您的路线模板:
[Route("api/tblProducts/{Price1}/{Price2}/{CategoryId}/{Color}/{Size}")]
因此,不是:
http://localhost:54393/api/tblProducts/GetsimlarProduct?Price1=1000&Price2=10000&CategoryId=Cat102&Color=Black&Size=M
使用:
更新:
如果要在结果为空的情况下找不到,还应更改If语句:
替换:
与:
我的方法代码由visual studio生成
public virtual ObjectResult<GetSimilarProducts_Result> GetSimilarProducts(Nullable<decimal> price1, Nullable<decimal> price2, string size, string categoryId, string color)
{
var price1Parameter = price1.HasValue ?
new ObjectParameter("Price1", price1) :
new ObjectParameter("Price1", typeof(decimal));
var price2Parameter = price2.HasValue ?
new ObjectParameter("Price2", price2) :
new ObjectParameter("Price2", typeof(decimal));
var sizeParameter = size != null ?
new ObjectParameter("Size", size) :
new ObjectParameter("Size", typeof(string));
var categoryIdParameter = categoryId != null ?
new ObjectParameter("CategoryId", categoryId) :
new ObjectParameter("CategoryId", typeof(string));
var colorParameter = color != null ?
new ObjectParameter("Color", color) :
new ObjectParameter("Color", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<GetSimilarProducts_Result>("GetSimilarProducts", price1Parameter, price2Parameter, sizeParameter, categoryIdParameter, colorParameter);
}
public virtual ObjectResult<GetSimilarProducts_Result> GetSimilarProducts(Nullable<decimal> price1, Nullable<decimal> price2, string size, string categoryId, string color)
{
var price1Parameter = price1.HasValue ?
new ObjectParameter("Price1", price1) :
new ObjectParameter("Price1", typeof(decimal));
var price2Parameter = price2.HasValue ?
new ObjectParameter("Price2", price2) :
new ObjectParameter("Price2", typeof(decimal));
var sizeParameter = size != null ?
new ObjectParameter("Size", size) :
new ObjectParameter("Size", typeof(string));
var categoryIdParameter = categoryId != null ?
new ObjectParameter("CategoryId", categoryId) :
new ObjectParameter("CategoryId", typeof(string));
var colorParameter = color != null ?
new ObjectParameter("Color", color) :
new ObjectParameter("Color", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<GetSimilarProducts_Result>("GetSimilarProducts", price1Parameter, price2Parameter, sizeParameter, categoryIdParameter, colorParameter);
}
更新:当您编辑我的答案时,我将在末尾添加解决方案:
由于存储过程没有返回值,所以在SSMS中运行时,可以在输出窗口中看到结果,但不会将任何内容返回到应用程序中。必须将存储过程更改为如下所示的存储函数:
USE [MakaAnOrderDB]
GO
/****** Object: StoredProcedure [dbo].[GetSimilarProducts] Script Date: 12/14/2017 12:44:09 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [dbo].[GetSimilarProducts]
(@Price1 decimal(18,0),@Price2 decimal(18,0),@Size nvarchar(max),@CategoryId nvarchar(255),@Color nvarchar(20))
As
Begin
Return Select top(20)* from tblProduct where PrdPrice Between @Price1 And @Price2 And PrdSize=@Size And PrdColor=@Color
End
GO
您应该遵循您的路线模板:
[Route("api/tblProducts/{Price1}/{Price2}/{CategoryId}/{Color}/{Size}")]
因此,不是:
http://localhost:54393/api/tblProducts/GetsimlarProduct?Price1=1000&Price2=10000&CategoryId=Cat102&Color=Black&Size=M
使用:
更新:
如果要在结果为空的情况下找不到,还应更改If语句:
替换:
与:
我的方法代码由visual studio生成
public virtual ObjectResult<GetSimilarProducts_Result> GetSimilarProducts(Nullable<decimal> price1, Nullable<decimal> price2, string size, string categoryId, string color)
{
var price1Parameter = price1.HasValue ?
new ObjectParameter("Price1", price1) :
new ObjectParameter("Price1", typeof(decimal));
var price2Parameter = price2.HasValue ?
new ObjectParameter("Price2", price2) :
new ObjectParameter("Price2", typeof(decimal));
var sizeParameter = size != null ?
new ObjectParameter("Size", size) :
new ObjectParameter("Size", typeof(string));
var categoryIdParameter = categoryId != null ?
new ObjectParameter("CategoryId", categoryId) :
new ObjectParameter("CategoryId", typeof(string));
var colorParameter = color != null ?
new ObjectParameter("Color", color) :
new ObjectParameter("Color", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<GetSimilarProducts_Result>("GetSimilarProducts", price1Parameter, price2Parameter, sizeParameter, categoryIdParameter, colorParameter);
}
public virtual ObjectResult<GetSimilarProducts_Result> GetSimilarProducts(Nullable<decimal> price1, Nullable<decimal> price2, string size, string categoryId, string color)
{
var price1Parameter = price1.HasValue ?
new ObjectParameter("Price1", price1) :
new ObjectParameter("Price1", typeof(decimal));
var price2Parameter = price2.HasValue ?
new ObjectParameter("Price2", price2) :
new ObjectParameter("Price2", typeof(decimal));
var sizeParameter = size != null ?
new ObjectParameter("Size", size) :
new ObjectParameter("Size", typeof(string));
var categoryIdParameter = categoryId != null ?
new ObjectParameter("CategoryId", categoryId) :
new ObjectParameter("CategoryId", typeof(string));
var colorParameter = color != null ?
new ObjectParameter("Color", color) :
new ObjectParameter("Color", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<GetSimilarProducts_Result>("GetSimilarProducts", price1Parameter, price2Parameter, sizeParameter, categoryIdParameter, colorParameter);
}
更新:当您编辑我的答案时,我将在末尾添加解决方案:
由于存储过程没有返回值,所以在SSMS中运行时,可以在输出窗口中看到结果,但不会将任何内容返回到应用程序中。必须将存储过程更改为如下所示的存储函数:
USE [MakaAnOrderDB]
GO
/****** Object: StoredProcedure [dbo].[GetSimilarProducts] Script Date: 12/14/2017 12:44:09 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [dbo].[GetSimilarProducts]
(@Price1 decimal(18,0),@Price2 decimal(18,0),@Size nvarchar(max),@CategoryId nvarchar(255),@Color nvarchar(20))
As
Begin
Return Select top(20)* from tblProduct where PrdPrice Between @Price1 And @Price2 And PrdSize=@Size And PrdColor=@Color
End
GO
另一种方法是,如果您想删除该长查询字符串:
另一种方法是,如果您想删除该长查询字符串:
我已经用过了,但这也不起作用,并且给出了相同的输出,输出是[]。请帮助我。你确定数据库中有适合的东西吗?看起来,这是你的问题。@Akshay Tomar如果用另一个可能的问题更新了我的答案。@Nikolus先生在数据库中,当我给出与应用程序相同的输入时,它工作正常,并给出理想的输出,但在我的web api应用程序中,它工作不正常,将给出一个空块[]就像那样,我使用了你更新的语法,但它不起作用。同样的output@AkshayTomar能否添加sql语句和GetSimilarProducts方法的代码?这对于找到问题的来源可能很重要。我已经使用了它,但它也不起作用,并且提供相同的输出,输出为[]。请帮助我。您确定数据库中有适合您的内容吗?看起来,这是你的问题。@Akshay Tomar如果用另一个可能的问题更新了我的答案。@Nikolus先生在数据库中,当我给出与应用程序相同的输入时,它工作正常,并给出理想的输出,但在我的web api应用程序中,它工作不正常,将给出一个空块[]就像那样,我使用了你更新的语法,但它不起作用。同样的output@AkshayTomar能否添加sql语句和GetSimilarProducts方法的代码?这对于找到问题的来源可能很重要。但这是一个get请求,我正在从过程中获取数据,然后如果我将其发布,那么它是否有效?您可以将get请求更改为post请求,然后仍然获取数据。Post将允许您将参数作为复杂对象发布,而不是发送查询字符串。这只是另一种方式。当您的URL超过URL字符数限制时,这会很有帮助。但这是一个get请求,我正在从过程中获取数据,然后如果我将其发布,那么它是否会工作?您可以将get请求更改为post请求,并且仍然获取数据。Post将允许您将参数作为复杂对象发布,而不是发送查询字符串。这只是另一种方式。当您的URL超过URL字符限制时,它会很有帮助。您还可以在c代码中添加调用吗?我是指您的工作procedure@Nikolus先生,我要更新我的c代码我怎么称呼它。你也可以添加db.SingleProductDetails-Method的代码吗?我想这是有区别的。@Nikolus先生,你是要C代码还是visual studio生成的代码,还是数据库存储过程代码?你能在C代码中添加调用吗?我是指你的工作procedure@Nikolus先生,我要更新我的c代码我怎么称呼它。你也可以添加db.SingleProductDetails-Method的代码吗?我想这是有区别的。@Nikolus先生,您是要C代码还是visual studio生成的代码,还是数据库存储过程代码?