Calling web service without using a post back - c#

I need to call a cross domain web service (.asmx) via a page within an asp.net application. The user will type in a search query in which the app will send off to the web service, then the page will be updated with the data.
My question is how might I do this without postbacks? As I am just trying to retrieve data rather than entering or manipulating it.
My attempted solutions:
1. Ajax via jquery -> issue: CORS is not supported.
2. serviceReference (for client side services calls) but this is only for a local (on domain) service
3. Lastly I am trying to use updatepanel where the value from a textbox is used in querying the web service however this is obviously forcing a partial postback, is there a way to have the update panel use a get req instead? If not, how might I go about doing this because I am stuck! Lastly would this solution be easier if an ASP MVC structure? I am learning. Thanks.

Create an ashx handler which queries the remote service based on parameters handed by ajax.
Use an ajax-request to the ashx handler to call your proxy via jQuery AJAX.
Or just use the Yahoo YQL proxy:
Is there a free JSON proxy server which supports CORS or JSONP?
Or if the web-service supports JSONP, you can use JSONP directly.
If you need to create a proxy yourself, here is how:
http://www.codeproject.com/Articles/25218/Fast-Scalable-Streaming-AJAX-Proxy-continuously-de

Related

Get notification in view when a web API called from an external system in c# MVC

I'm developing a website in C# MVC including WEB API. When an API URL in my project is called from an external system, I want to show a message (not a push message, only show a text in a div) in one of my view that the API function is executing.Is this possible?
First Of All its possible yes,
you need To use SignalR To notify the Views:
YouTube totrual Here
Github : Here
Then You use the Signal R to push a Notification when a cross origin request happen
using action filters
the signalR subscribers can be view pages using Jquery.
SignalR on Client
full exmaple :here
Yes it is posssible. One (pretty easy) way is to use the awesome SignalR or SignalR core framework. Check this for differences. Real-time web functionality enables server-side code to push content to clients instantly.
Basically you will create a Hub on the server that clients connect to. In your WebApi method, you can then call the client method. Then in the client you will use javascript to respond to the server call and then you will set the div content from this method.
See the docs.
Hope it helps!
I think Signal R is best choise too. But maybe you want to another alternative. You can look at Node Js.
Node js.org
General Tutorial
General Tutorial 2
For .NET Tutorial

Keeping track of internal applications using ASP.NET Web APIs (Logging)

I currently have a lot of Web APIs that are being used by various applications. All of the APIs are using ASP.NET Web API 2 with 4.6 .NET framework. I want to be able to keep track of all the different applications that are using a specific API.
Example:
Lets say I have the following API:
Employees API - gets all employee(s)
And I have the following applications that call the Employees API
Employee Management System - located at /intranet/employee/add
Time Off Request Form - located at /intranet/timeoff.aspx
The Employee Management System calls the Employees API using HttpClient in C# code.
The Time Off Request Form calls the Employees API using an ajax call in JavaScript.
I want to keep a log of all the applications that are using a Web API and preferably what action they are calling. What is the best way to achieve this?
Is there a way to get the Callers URI?
From the Web API is it possible to know that /intranet/timeoff.aspx is calling the the add employee action on the Employee API
I believe you have a few options.
Add a header value to identify the application to each request from the applications.
Add a claim identifying the application to a security token
(assuming you are securing the requests).
Add a query parameter for the requesting application to each request.
If you have control of the applications that call your API, then Michael_B
offers some good suggestions. If not then you may need to consider some referring information like IP address - see How to get IpAddress and UserAgent in ASP.NET Web API get methods. However, even then that value may not be the client IP but something in between it and your API.
Nevertheless, since you're using Web API, you might consider running all of the requests through a DelegatingHandler first, write the caller info, etc. to a log from there, then let the request continue to the proper controller. See http://arcware.net/logging-web-api-requests/

Difference between calling a web service from an ajax call and calling it from an MVC Controller

I have an MVC 5 project with an OData V4 web service for the backend, and I'm starting to run into some confusion when it comes to retrieving data (not views).
I'm not sure whether I should be making my web service calls through ajax or through an MVC controller action with an HTTP client. I know you typically want some additional separation in the latter case in the sense that the controller action probably shouldn't be directly calling the web service with an HTTP client, but regardless. I should also reiterate that my question has nothing to do with Views/PartialViews; I always call a controller action to return those.
Is it only advantageous to go through a controller action when you need to perform additional work on the data being returned?
If I'm simply retrieving a list of objects from the web service, is there any point in taking that extra step by calling a controller action?
Sorry if this has been answered before. I found some vaguely similar questions but nothing too concrete.
Thanks.
If the web service resides in a domain other than your web app domain, you probably will run into same origin policy issue when trying to make ajax call from your pages. Basically this is not a problem, but a security feature where a web browser permits scripts contained in web page A to access data in a web page B, but only if both web pages have the same origin. An origin is defined as a combination of URI scheme, hostname, and port number.
If you have this problem, you can try to access the web service from your server code(controllers/services). This also allows you to parse the response coming back and converting to another format which your view really want. If you are accessing data which won't be changed very often, you may cache it also using MemoryCache or other dot net caching solutions. This will save you some network calls and improve your page load time.

Server control library calling its own services

I am posting this as a part of my effort in searching the best possible design solution for my requirement.
I am currently working on a complex server control(not user control) in asp.net which is going to be rendered into html elements on the client side.And those html elements needs to do a ajax call backs to the server using js/jquery. Here is the problem. As this is a serverside control and can be added into any application/domain. I dont want to have those callback services hosted separately. Is there any way that I can host those server callback services in the same library? If so, how can I access them from the client side?
I've never tried putting a WebService into a ServerControl, but I know you can create a composite control (a server control that inherits form the CompositeControl class) that has an update panel in it. With that you can do server side processing with asynchronous calls from the client, similar to JQuery calling a WebService.

Getting Generated HTML in a WCF service

In the WCF application that I am working on, I need to access the generated source of a particular webpage (after all the AJAX calls on the page are made).
I have tried using System.Net.WebRequest but it just brings me back the original source of the page. Is there a way to execute a page and then get the source?
Else, is there a way to execute Javascript from within a WCF service? I could use the javascript and JSON response to create the HTML page from within my webservice then!
You could use Javascript to traverse and pass the DOM than make a call into your WCF service from the Javascript when all the Ajax calls are complete. If you are after the data that is stored on the page after all the Ajax calls I would re-think your implementation...
Petar
Well, WCF is designed to be consumed by non-browsers, so there really is no way to expect that a WCF response can contain Javascript that will be automatically executed by the client.
#Petar: Thanks for your input. Yes, I am after that data that will be stored in the page after the Ajax calls. And, somehow the third party vendor will not give me that data via some JSON calls which I could directly call from my own WCF service.

Categories

Resources