call my web services from other app with javascript? - asp.net

I got .asmx a web service on my app. I need to call a method from an other app to get statistics from my app. I need it to return XML. the call to the webmethod is done with javascript soap.
EDIT
I got the web service working. I can execute code and return a string but it stops there. When I try to pass parameters into the method it wont work and when I try to return a string[] or any other type it wont work either. any ideas? Is there something I need to do passing in parameters?

I think you could do two things.
One: enable [ScriptService] attribute on web method. This allows you to call the webservice
with javascript.
Example
Two: enable http-post/http-get webservice calls
How to enable

Related

Resource based (Imperative) authorization won't enter the handler

Scenario:
I have an API with .net core 2.2
On top my controller I authorize access using IdentityServer4 with an Attribute
Inside one of my endpoints I want to authorize access to a method only in some cases
I implemented resource based authorization inside my endpoint just like it's shown in microsoft documentation.
It didn't work.
I put a breakpoint inside my authorization handler and tried debugging, but when this handler should be called, it is not.
I mean that when the following line runs
var authorizationResult = await _authorizationService
.AuthorizeAsync(User, Document, "EditPolicy");
the Handler should be called, but that never happens.
Did anyone have the same problem?
So in the end the problem was due to the registration of the service in the startup.cs.
I was using TryAddScope, by changing to AddScope it worked fine.

Difference between wcf and web api uri definition

I want to convert our existing WCF REST web services to ASP.NET Web APIso I started to look into it.
Getting one of my function (i.e. login) up and running in ASP.NET Web API was quite straight forward but there is one thing I'm confused about and I hope one of you can clarify this for me.
In our WCF REST web service, our login (POST) function was called as follows:
http://localhost/mywebsite/mywebservice.svc/Authentication/Login
We'd pass a LoginRequest to it and we'd get a LoginResponse back.
Now in ASP.NET Web API, I've our Login (POST) function is being called as follows:
http://localhost/api/authentication and I'm passing the same LoginRequest and I get the same LoginResponse.
My confusion is, how does ASP.NET Web API know to use the Login function which is defined in the AuthenticationController?
I assume it has something to do with the parameter type being passed but what if I have another function that has the same parameter type, how would it differentiate between the 2?
For example, what if I had a LocalLogin and CloudLogin (not the case btw) and both require the LoginRequest as an input parameter and both return the LoginResponse, how would it know which one to call since it's not part of the URI?
Thanks.

Server side callback function ajax web service asp.net

I'm using Web Service to handle ajax request in my project. I need to call a server side function to count users online when a new request is proceed.
If you want to call Server side method into Client side then,you have to transform this method as a PageMethod and then call this method i.e GetOnlineUser() from client side code; i.e. using JavaScript.
To enable the method as a PageMethod, add the attribute [WebMethod] on top of the GetOnlineUser method in .aspx code behind file.
if you are using asp.Net membership provider then simply call Membership.GetNumberOfUsersOnline().
And if you are not using membership you have to implement your own custom counter...

How Do I Get RouteData Values from a Web Service in .Net 4.0

I am trying to extract an id number from a URL using a web service so that it can be used as a parameter for a where clause in a select statement that produces data from a database based on the id number of a record. That data will then be passed back to the page to populate an element in a jQuery modal popup widow.
Everything works fine with a static id number (ex: string postid = "120"), but I don't know how to get the id number from the URL. I'm using Routing in .Net 4 and the method for accessing Routing in pages does not work in a web service. In pages I just do stuff like var id = RouteData.Values["id"]; and that gets the id, but when i did it in a web service I got an error:
CS0120: An object reference is required for the non-static field,
method, or property 'System.Web.Routing.RouteData.Values.get'
Summary:
I have web service accessed form a details page where I want to get RouteData for the page making the request. I want to do this just as easily as I can on a page using RouteData.Values which is just as easy as the now the obsolete Request.Querystring.
Now I am more confused because although I could easily add a new route for the web service I don't know I would call that using jQuery Ajax because of the webservice.asmx/webmethod syntax.
Right now I have URL: "../webservices/googlemaps.asmx/GetGoogleMap" in my jQuery Ajax, but that is not a real URL. It only exists in jQuery somewhere and the way to call the service using just JavaScript is no a real URL either, its webservice.webmethod() which in this case would be googlemaps.GetGoogleMap().
I will try registering a route for webservices/googlemaps.asmx/GetGoogleMap/postid, but I doubt it will work because GetGoogleMap is not a directory or a querystring.
Get current http request and use RequestContext property to get request context - it has current routing data. For example,
var id = HttpContext.Current.Request.RequestContext.RouteData.Values["id"];
In case of WCF based web service, make sure that service is participating in ASP.NET pipeline (see ASP.NET Compatibility)
EDIT: Sorry for misleading answer - the above will not work unless web service url is registered in routing engine. However, it may not solve your issue of retrieving the id - what kind of service implementation are you using? Are you making a GET request or POST request? Typically, web service handler (asmx) or WCF pipeline should convert GET/POST parameters to method parameters. Post your web service code and how you invoke it.

How to call a webmethod asynchronously and partially render a control?

I need to call a webmethod of a webservice asynchronously from code behind of a web page.
In the callback function I need to bind a gridview and render it. I want to partially render that gridview in the callback function in codebehind.
How to implement that?
Is it possible to implement all these in codebehind without using javascript?
There are a couple of options, but basically you need to do something like this:
Use Visual Studio to build a proxy class to access the web service, using the published WSDL
Create an async web page, by setting Async=True in the Page directive
In the Page_Load() method of your code behind, register methods that will start and end the async web service call by creating a PageAsyncTask object and calling RegisterAsyncTask()
From the method that starts the async task, call the Begin method that was created as part of the proxy class, and return the associated IAsyncResult to the caller
When the web service call completes, the runtime will call your registered end method. From there, call the End method in the proxy to get the results of the call.
Databind the results to a GridView on your page.
In case it helps, I walk through a detailed example along these lines in my book, including sample code: Ultra-Fast ASP.NET.
You can use ASP.NET asynchronous page load for that.
In general, it consists of adding Async="true" to the page directive and adding some event handlers in the code behind.
Great resource about this subject is the "Asynchronous Pages in ASP.NET 2.0" MSDN Magazine article.

Resources