I am developing a ASP.NET Web Api and a ASP.NET Website. The website will make use of the Web Api and a mobile app will also be using the Web Api via REST.
我正在开发ASP.NET Web Api和ASP.NET网站。该网站将使用Web Api,移动应用程序也将通过REST使用Web Api。
Developing these two separately is going fine, however I am now at the stage where I would like to start testing the Web Api from the Website, ideally all from within visual studio. For instance, I have a page where I have a form, that when completed would call my Web Api to add a user to the database.Uploading these online for testing is naturally out of the question.
分别开发这两个很好,但我现在正处于我想从网站开始测试Web Api的阶段,理想情况下,所有这些都来自visual studio。例如,我有一个页面,我有一个表单,完成后会调用我的Web Api将用户添加到数据库。上传这些在线进行测试自然是不可能的。
So what is the best practice here? Can you simply reference the Web Api from within the Website project (Aspx) or is there another way to go about this.
那么这里最好的做法是什么?你能简单地从网站项目(Aspx)中引用Web Api,还是有另一种方法可以解决这个问题。
3 个解决方案
#1
6
Access to Web API controllers and actions are based on urls. So now that they are on separate projects you need to run both projects at the same time to make your API available for MVC project.
对Web API控制器和操作的访问基于URL。因此,现在它们处于单独的项目中,您需要同时运行这两个项目,以使您的API可用于MVC项目。
and by the way you should enable CORS for your web api project so that you can access it from your MVC project.
顺便说一句,您应该为您的web api项目启用CORS,以便您可以从MVC项目访问它。
#2
3
Use the below code to consume web api project
使用以下代码来使用web api项目
public async Task <ActionResult> Index()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:54568/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("api/Values/"); //API controller name
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsAsync<YourReturnDataType>();
if (result != null)
var output = result;
}
}
return View("Return your model here");
}
#3
1
It depends what you want to test. If you want to simply test your controller implementations you can create a test project and reference the project, manually instantiate your controllers, and invoke them in your tests.
这取决于你想要测试的内容。如果您只想测试控制器实现,可以创建测试项目并引用项目,手动实例化控制器,并在测试中调用它们。
If you want to do integration tests over the network, you can self-host the web api service. Then install the web api client package Install-Package Microsoft.AspNet.WebApi.Client
in the test project and invoke via the client.
如果要通过网络进行集成测试,可以自行托管Web api服务。然后在测试项目中安装web api客户端软件包Install-Package Microsoft.AspNet.WebApi.Client,并通过客户端调用。
Example linking and manual instantiation (from link):
示例链接和手动实例化(来自链接):
[TestClass]
public class TestSimpleProductController
{
[TestMethod]
public void GetAllProducts_ShouldReturnAllProducts()
{
var testProducts = GetTestProducts();
var controller = new SimpleProductController(testProducts);
var result = controller.GetAllProducts() as List<Product>;
Assert.AreEqual(testProducts.Count, result.Count);
}
Example using the web api client (from link):
使用web api客户端的示例(来自链接):
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:9000/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// New code:
HttpResponseMessage response = await client.GetAsync("api/products/1");
if (response.IsSuccessStatusCode)
{
Product product = await response.Content.ReadAsAsync>Product>();
Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category);
}
}
For self hosting the service:
对于自托管服务:
http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api
http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api
For the Web Api client:
对于Web Api客户端:
Install-Package Microsoft.AspNet.WebApi.Client
#1
6
Access to Web API controllers and actions are based on urls. So now that they are on separate projects you need to run both projects at the same time to make your API available for MVC project.
对Web API控制器和操作的访问基于URL。因此,现在它们处于单独的项目中,您需要同时运行这两个项目,以使您的API可用于MVC项目。
and by the way you should enable CORS for your web api project so that you can access it from your MVC project.
顺便说一句,您应该为您的web api项目启用CORS,以便您可以从MVC项目访问它。
#2
3
Use the below code to consume web api project
使用以下代码来使用web api项目
public async Task <ActionResult> Index()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:54568/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("api/Values/"); //API controller name
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsAsync<YourReturnDataType>();
if (result != null)
var output = result;
}
}
return View("Return your model here");
}
#3
1
It depends what you want to test. If you want to simply test your controller implementations you can create a test project and reference the project, manually instantiate your controllers, and invoke them in your tests.
这取决于你想要测试的内容。如果您只想测试控制器实现,可以创建测试项目并引用项目,手动实例化控制器,并在测试中调用它们。
If you want to do integration tests over the network, you can self-host the web api service. Then install the web api client package Install-Package Microsoft.AspNet.WebApi.Client
in the test project and invoke via the client.
如果要通过网络进行集成测试,可以自行托管Web api服务。然后在测试项目中安装web api客户端软件包Install-Package Microsoft.AspNet.WebApi.Client,并通过客户端调用。
Example linking and manual instantiation (from link):
示例链接和手动实例化(来自链接):
[TestClass]
public class TestSimpleProductController
{
[TestMethod]
public void GetAllProducts_ShouldReturnAllProducts()
{
var testProducts = GetTestProducts();
var controller = new SimpleProductController(testProducts);
var result = controller.GetAllProducts() as List<Product>;
Assert.AreEqual(testProducts.Count, result.Count);
}
Example using the web api client (from link):
使用web api客户端的示例(来自链接):
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:9000/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// New code:
HttpResponseMessage response = await client.GetAsync("api/products/1");
if (response.IsSuccessStatusCode)
{
Product product = await response.Content.ReadAsAsync>Product>();
Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category);
}
}
For self hosting the service:
对于自托管服务:
http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api
http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api
For the Web Api client:
对于Web Api客户端:
Install-Package Microsoft.AspNet.WebApi.Client