我在ASP.NET Web API应用程序中使用多个输入参数,但我没有得到输出。
我的代码在这里:
[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);
}
我使用给定的URI来访问它
http://localhost:54393/api/tblProducts/GetsimlarProduct?Price1=1000&Price2=10000&CategoryId=Cat102&Color=Black&Size=M
请帮助我如何从数据库和我的其他存储过程中获取数据 我的方法代码由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);
}
这是我的存储过程代码
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 procedure [dbo].[GetSimilarProducts]
(@Price1 decimal(18,0),@Price2 decimal(18,0),@Size nvarchar(max),@CategoryId nvarchar(255),@Color nvarchar(20))
As
Begin
Select top(20)* from tblProduct where PrdPrice Between @Price1 And @Price2 And PrdSize=@Size And PrdColor=@Color
End
GO
更新:我的存储过程运行良好
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);
}
update:visual studio为程序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);
}
答案 0 :(得分:1)
您应该按照路线模板:
[Route("api/tblProducts/{Price1}/{Price2}/{CategoryId}/{Color}/{Size}")]
所以而不是:
http://localhost:54393/api/tblProducts/GetsimlarProduct?Price1=1000&Price2=10000&CategoryId=Cat102&Color=Black&Size=M
使用:
http://localhost:54393/api/tblProducts/1000/10000/Cat102/Black/M
更新: 如果您想在空结果的情况下找不到,您还应该更改您的if语句: 替换:
if (tblSmlrProduct == null)
使用:
if (tblSmlrProduct == null || tblSmlrProduct.Count()==0)
我的方法代码由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);
}
这是我的存储过程代码
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 procedure [dbo].[GetSimilarProducts]
(@Price1 decimal(18,0),@Price2 decimal(18,0),@Size nvarchar(max),@CategoryId nvarchar(255),@Color nvarchar(20))
As
Begin
Select top(20)* from tblProduct where PrdPrice Between @Price1 And @Price2 And PrdSize=@Size And PrdColor=@Color
End
GO
更新:在您编辑我的答案时,我将在最后添加解决方案: 由于存储过程没有返回值,因此您可以在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
答案 1 :(得分:0)
另一种方法,如果你想摆脱那个长查询字符串: