Asp.net mvc Asp.NETMVC使用类库中的web api
我想知道是否有一种方法可以创建一个包含web API的类库,这样我就可以使用dll作为插件,将其注入到MVC应用程序中Asp.net mvc Asp.NETMVC使用类库中的web api,asp.net-mvc,plugins,asp.net-web-api,class-library,Asp.net Mvc,Plugins,Asp.net Web Api,Class Library,我想知道是否有一种方法可以创建一个包含web API的类库,这样我就可以使用dll作为插件,将其注入到MVC应用程序中 提前感谢您的帮助。Web API方法是通过HTTP调用的,因此调用Web API服务需要将其托管在某个位置,并使用适当的客户端进行调用,如中所述。可以,因此理论上可以有一个MVC应用程序的本地程序集,该程序集包含一个类,该类设置并调用自托管Web API服务 您可以在接口后面注入Web API服务,如下所示: public interface IProductsService
提前感谢您的帮助。Web API方法是通过HTTP调用的,因此调用Web API服务需要将其托管在某个位置,并使用适当的客户端进行调用,如中所述。可以,因此理论上可以有一个MVC应用程序的本地程序集,该程序集包含一个类,该类设置并调用自托管Web API服务 您可以在接口后面注入Web API服务,如下所示:
public interface IProductsService
{
IEnumerable<Product> GetAllProducts();
}
public class SelfHostedWebApiProductsService
{
public SelfHostedWebApiProductsService()
{
// Set up a self-hosted Web API service
}
public IEnumerable<Product> GetAllProducts()
{
// Call your self-hosted WebApi to get the products
}
}
公共接口IPProductsService
{
IEnumerable GetAllProducts();
}
…实现了如下内容:
public interface IProductsService
{
IEnumerable<Product> GetAllProducts();
}
public class SelfHostedWebApiProductsService
{
public SelfHostedWebApiProductsService()
{
// Set up a self-hosted Web API service
}
public IEnumerable<Product> GetAllProducts()
{
// Call your self-hosted WebApi to get the products
}
}
公共类selfHostedWebAPI产品服务
{
公共自宿EBAPIProductsService()
{
//设置自托管Web API服务
}
公共IEnumerable GetAllProducts()
{
//调用您的自托管WebApi以获取产品
}
}
要在ipProductsService
界面中使用selfhostedWebAPI产品服务
,您可以离开。详细说明如何设置和调用自托管Web API
作为<代码> SelfHooStdWebAPiPixStudioService < /Cuff>在其构造函数中设置了自托管的Web API——一个相对昂贵的操作——您可能需要考虑在DI容器中赋予该类一个单独的生命周期。 < P>您可以自己承载一个Web API: 可以将所有控制器放在类库项目中 然后使用Autofac解决宿主项目中的依赖关系:
谷歌似乎工作得很好:也许你只是想把你的asp.net mvc控制器和webapi控制器分开?然后检查此so帖子: