简体   繁体   中英

Testing WebApi availability from Javascript or C#

My ASP.NET MVC 4 website connects a external WebApi service. 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.

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.

So I thought about doing it with the Jquery Ajax method.

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. 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. 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. Just have the javascript functions associated with the success and error events on the ajax method. If it hits success function then the validation is successful; if it hits the error function then validation fails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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