Web API 2不从数据库中检索数据 [英] Web API 2 is not retrieving data from database

查看:100
本文介绍了Web API 2不从数据库中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有api控制器,必须根据ISBN检索数据库中的数据



当我收到错误时



:没有找到与请求URI匹配的HTTP资源'http:// localhost:2280 / api / ServiceA /'。,MessageDetail:否在与服务请求匹配的控制器'ServiceA'上找到了操作。} 





我尝试过:



这是控制器类,它必须根据网址中的isbn过滤数据。但是显示没有http资源匹配UrI

 [Route({isbn})] 
public Book Get(string isbn)
{

using(AppDbContext db = new AppDbContext ())

{

var query =来自db.Books中的b

其中b.ISBN == isbn&& b。来源==书店1

选择b;

return query.SingleOrDefault();

}

}
}
}





这是桌子书



公共课本
{
[表(书籍)]
public class Book
{
public int Id {get;组; }
公共字符串ISBN {get;组; }
public string Title {get;组; }
public string Publisher {get;组; }
public string作者{get;组; }
公共小数价格{get;组; }
public string Source {get;组; }
public string PurchaseUrl {get;组; }
}
}

解决方案

添加 [RoutePrefix(api / ServiceA)] 控制器的属性。如果没有它,不知何故,有效的URI将是http:// localhost:2280 / isbn而不是http:// localhost:2080 / api / ServiceA / isbn



 [RoutePrefix(  api / ServiceA)] 
public class ServiceAController:ApiController



我有没有RoutePrefix属性的相同错误

 <  错误 >  
< 消息 > 未找到与请求URIhttp:// localhost:47503 / api / ServiceA / 2匹配的HTTP资源。< /消息 >
< span class =code-keyword>< MessageDetail > 在控制器'ServiceA'上找不到与请求匹配的操作。< / MessageDetail >
< span class =code-keyword>< /错误 >


I have api controller in my project which has to retrive data from database based on ISBN

When I am getting an error

":"No HTTP resource was found that matches the request URI 'http://localhost:2280/api/ServiceA/'.","MessageDetail":"No action was found on the controller 'ServiceA' that matches the request."}



What I have tried:

Here is the controller class which have to filter the data based on isbn in the url. but instead its showing no http resource matches the UrI

       [Route("{isbn}")]
       public Book Get(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();

            }

        }
    }
}



Here is the table book

public class book
    {
        [Table("Books")]
        public class Book
        {
            public int Id { get; set; }
            public string ISBN { get; set; }
            public string Title { get; set; }
            public string Publisher { get; set; }
            public string Author { get; set; }
            public decimal Price { get; set; }
            public string Source { get; set; }
            public string PurchaseUrl { get; set; }
        }
    }

解决方案

Add a [RoutePrefix("api/ServiceA")] attribute to the controller. Without that, somehow, the valid URI will be http://localhost:2280/isbn instead of http://localhost:2080/api/ServiceA/isbn

[RoutePrefix("api/ServiceA")]
    public class ServiceAController : ApiController


I got the same error without the RoutePrefix attributes

<Error>
  <Message>No HTTP resource was found that matches the request URI 'http://localhost:47503/api/ServiceA/2'.</Message>
  <MessageDetail>No action was found on the controller 'ServiceA' that matches the request.</MessageDetail>
</Error>


这篇关于Web API 2不从数据库中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆