简体   繁体   English

从Javascript或C#测试WebApi可用性

[英]Testing WebApi availability from Javascript or C#

My ASP.NET MVC 4 website connects a external WebApi service. 我的ASP.NET MVC 4网站连接了外部WebApi服务。 I have a page where I set the BaseAddress Uri and consequently I want to know the WebApi service status everytime the BaseAddress is changed. 我有一个页面,我在其中设置了BaseAddress Uri,因此,我想每次更改BaseAddress时都知道WebApi服务状态。

I don't want my page to be blocked while this status if being checked. 如果选中此状态,我不希望我的页面被阻止。

I tried to do it on the controller side, but for that I need to have my Api Call with an Async Task, however I don't know exactly how to get deplayed response. 我尝试在控制器端执行此操作,但是为此,我需要使用异步任务进行Api调用,但是我不确切知道如何获得延迟的响应。

So I thought about doing it with the Jquery Ajax method. 因此,我考虑过使用Jquery Ajax方法进行操作。

My question is : what is the correct place to do this ? 我的问题是:这样做的正确位置是什么? And if on the controller, how I will get my result ? 如果在控制器上,我将如何获得结果?

Thanks 谢谢

It makes sense to do this on the client side using the jQuery ajax method. 在客户端使用jQuery ajax方法执行此操作很有意义。 I think it is best practice to do any client-side validation when you can to avoid round trips to the server. 我认为最好的做法是进行任何客户端验证,以免往返于服务器。 If I understand what you are trying to do, it is to validate that the user has entered a valid base URI that points them to your Web API. 如果我了解您要尝试做的事情,那就是验证用户是否输入了有效的基本URI,该URI将他们指向您的Web API。 This validation can easily be done on the client with jQuery ajax as long as the Web API call you make does not update any state information on the server. 只要您进行的Web API调用不会更新服务器上的任何状态信息,就可以使用jQuery ajax在客户端上轻松完成此验证。 Just have the javascript functions associated with the success and error events on the ajax method. 只需将与ajax方法上的成功和错误事件相关联的javascript函数。 If it hits success function then the validation is successful; 如果达到成功功能,则验证成功;否则,验证成功。 if it hits the error function then validation fails. 如果遇到错误函数,则验证失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM