在Web API 2中获取null值而不是数据 [英] Getting null value instead of data in web API 2
本文介绍了在Web API 2中获取null值而不是数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用此URI
http:// localhost:2259 / api / ServiceA / 1234
其中isbn = 1234 isbn value在数据库中是1234
我得到Null。虽然我在数据库中有数据应该在这里显示
我尝试过:
我的webapiconfig
with this URI
http://localhost:2259/api/ServiceA/1234"
where isbn=1234 isbn value is 1234 in database
I am getting Null . While i have data in the database which should be shown here
What I have tried:
my webapiconfig
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{isbn}",
defaults: new { isbn = RouteParameter.Optional }
);
}
和我的服务A控制器
and my service A Controller
public class ServiceAController : ApiController
{
[HttpGet]
public Book GetBook(string isbn)
{
using (AppDbContext db = new AppDbContext())
{
var query = from b in db.Books
where b.ISBN == isbn && b.Source == "Book Store 1"
select b;
return query.SingleOrDefault();
}
}
}
推荐答案
嗯,你得到null因为你的查询WHERE条件与任何记录都不匹配。
使用行在上设置断点并检查
isbn的值
。机会很好,它的价值不是你想象的那样。
Well, you're getting null because your query WHERE condition doesn't match any records.
Put a breakpoint on theusing
line and check the value ofisbn
. Chances are good its value is not what you think it is.
这篇关于在Web API 2中获取null值而不是数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文