如何在我的MVC 4项目中使用IIS上托管的web api服务 [英] How can consume web api service that is host on IIS in my MVC 4 project

查看:113
本文介绍了如何在我的MVC 4项目中使用IIS上托管的web api服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在IIS上托管的web api mvc服务。如何使用ajax在我的新mvc项目中使用我的服务



我尝试过:



i尝试通过添加web添加服务引用在我的mvc项目上添加托管服务并将我的URLhttp:// localhost:8080 / api / casestatusall但我发现错误,如无法打开此互联网站点。请求的站点要么不可用,要么找不到。请稍后再试。

i have web api mvc service that host on IIS. How can consume my service in my new mvc project using ajax

What I have tried:

i trying to add hosted services on my mvc project through add web "add service refrence" and put my url "http://localhost:8080/api/casestatusall" but i found error like "unable to open this Internet site. the requested site is either unavailable or can not be found. Please try again later."

推荐答案

就像在任何其他应用程序中使用它一样;控制台,WPF等所有你需要的是一个 HttpClient 对象,其余的都记录在案。 :-)就像你提到的那样,你想在localhost:8080打开它,你确定这是你当前主持的API的位置吗?通常情况下,除非您想覆盖所有内容,否则会有一个不同的端口(,您永远不应该这样做!)。

Just the way you will consume it in any other application; console, WPF etc. All that you would need is an HttpClient object and the rest is all documented. :-) Like you have mentioned, you wanted to open it up at "localhost:8080", are you sure that is the location where your API is currently hosted at? Typically, there is a different port unless you want to override everything (which, you should never do!).
引用:

我试图通过添加webadd service refrence来添加我的mvc项目上的托管服务

i trying to add hosted services on my mvc project through add web "add service refrence"

您将Web API与WCF混淆; Web API不需要那么多愚蠢的东西,添加服务引用等,你需要的只是一个HttpClient而且你已经完成了。其余的东西由HTTP协议处理,您可以轻松使用API​​,例如以下方法可以使用,

You are confusing Web API with WCF; Web API does not need that much of stupid stuff, to adding service references etc, all you need is an HttpClient and you're done. Rest of the stuff is taken care of by the HTTP protocol and you can consume the API easily, for example the following would work,

var client = new HttpClient();

client.BaseAddress = new Uri("http://localhost:8080/api");

// get something
var something = client.GetAsync("casestatusall");

// Format the response to get the object etc
// You're done.



看看我的文章,他们也会指导你这个主题。

提示ASP.NET MVC框架中的Ajax开发人员 [ ^ ]

ASP.NET 5 Web API RESTful CRUD和Windows 10本机应用程序 [ ^ ]



还有更多,您可以查找有关HttpClient对象的正确文档以及HTTP协议的工作原理。


Have a look at my articles as well, they will guide you on this topic as well.
A Tip for Ajax Developers in ASP.NET MVC Framework[^]
ASP.NET 5 Web API RESTful CRUDs and Windows 10 native application[^]

For more as well, you can look for a proper documentation on HttpClient object and how the HTTP protocol works.


这篇关于如何在我的MVC 4项目中使用IIS上托管的web api服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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