0

I have a web app that is created using asp.net core. In the future I would like to use the exact same data from that web app, and possibly create a xamarin app.

Is it a good idea to just create an action that queries the data and passes it to the view as json, then use the xamarin app to download that json from the url? Or do I need an actual api?

If so, can I add the authentication from the web app, and keep that same authentication for the xamarin app, and that will allow access to that url? Kind like an api key, but just regular log in authentication.

1 Answers1

0

I would simply add the web service call to the existing web page, or perhaps create a separate web service page that you use to handle the calls.

Once of the really great features of asp.net is that all you really have to do is create public function in the web page, and you off to the races.

so, your code behind is just written as regular code.

<WebMethod()>
Public Shared Function SaveNames(FirstName As String, LastName As String) As String

    ' do whatever with first name and last name

    Return "ok"

End Function

And now you JavaScript call (jQuery for ease) would be this:

eg:

    $.ajax({  
        type: "POST",  
        url: 'WebForm1.aspx/SaveNames',
        contentType: "application/json",  
        datatype: "json",
        data: '{FirstName: "Albert", LastName: "Kallal"}',

        success: function(responseFromServer) {  
            alert(responseFromServer.d)  
        }  
    });  

Note how the 2 parameters in the code behind match the 2 values I sent.

So, the data sent to a function behind the 7scenes can jason, or text or xml, but asp.net web figures this out, and the function above has two strong typed parameters to the function (FirstName and LastName).

When you create the above public function? Well, you instant get a SOAP call.

But you also get a rest call with above also.

So, MywebUrl/MyWebPage.aspx?&FirstName="Albert"&LastName="Kallal"

So, you can use a URL and a typical rest call like above URL with parameters, or you can do a full SOAP call like the jQuery example. So any web service call you setup supports both a standard post (rest) approach, or a full SOAP and xml or jason call. The choice is yours without extra effort. So the above example will work with XML/SOAP, and it also woks as rest (url) and parameters. Note how the call used json, but asp.net figures this out, and pulled the json apart into the 2 named and strong typed parameters of the function.

So, yes, from Android or a web client? I would most certainly expose some web service methods from code behind. In above, that simple code stub can be placed into a existing web page. So for a web application, then I'll often just add a few public functions or subs to the existing web page, and I am done.

For a phone? Well, then of course add an actual web services page, and the above same code will work. (just change the url to the asmx page, but the rest of the code is the same. So, it not a lot of work - only requires you type in a public function with the WebService directive and you have a workable interface that any web browser or phone app can now use.

With just a few parameters then above as a REST/url is fine. If there was to be many parameters, then I would not use a rest call (build up the url and concentrate the messy parameters to that url), and would call as above per the jQuery example.

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
  • This seems simple enough, I will need to do some more reading, but how can I pass authentication? could/should I pass email/password in the parameters? Maybe that is what you were implying. – stupidQuestions Jul 09 '20 at 20:56
  • For web application you assume user is logged in. Web pages (or asmx web service) are thus placed in a folder that requires authentication - you don’t have to do anything at all – it will just work. So for web apps we assume user is already logged in. The same security for web pages that require authenticated users will thus work. If you place the web page in a folder that does not require a logged in user, then no user/password is required. If this is a web service call from a device etc? then use this: https://stackoverflow.com/questions/5507234/use-basic-authentication-with-jquery-and-ajax – Albert D. Kallal Jul 10 '20 at 01:16