How to use WebApi in visual studio - c#

I'm developing a project that based on database columns selection, will present those columns as charts in a browser web page.
i have been reading about WebApi (i've been told to use it with a windows form project) and for my problem i can't figure out which is the better (easy) aprouch.
The charts are created with javascript and in with the windows form i collect the database connection values (server, username,...) and the columns that will be selected for the charts.
My questions are:
do i call the webapi from javascript sending the column name and receving the values to use?
if so how can i send the column name from my windows form to javascript?
do i call it from my windows form and do all the work in the webapi and present it using the cshtml files that the webapi has?
if so how can i call the webapi from my windows form (present the cshtml file)?
how to send the data i need (columns names) to it? (i've seen also HttpClient)

Your web service (implemented by a Web API project) will return the data (e.g. comma-separated, JSON, or XML) to your local app (a web service per definition has no UI, otherwise it would be a website app). What your local app then does with the retrieved data, is a completely other thing.
And yes, it's of course possible to call a web service from JS. A web service URL is just a normal URL and can be handled like any other one. Google is your friend on that - you may use jQuery, it has some convenient shortcuts for this task.

Related

How to create a Web Page Client for a Web Api Service in .NET?

in this moment I have a Web Api Service but I´m trying to create a simple page for that WEB API. The problem is that I don´t know how is the best ideia for doing that, for example I can create a angular project but it will take me time because i dont know how to use angular and I´m using visual Studio 2022 (not VSCode).
This photo is a illustration of i need to do
My problem also is how to call data for a web api (ex: i create a button and that calls a controller for a web api) can someone give me the documentation/help to "connect" both projects. The next photo shows the solution complete (ClientProject is a empty Razor template page where I need to edit the pages where I have to call the web api (I just don't know how to do it) and the ServerProject is the Web Api with a lot of stuff that I want to reuse)
Any help is welcome
You do not "connect" both applications in Visual Studio.
Although the applications are in the same solution, you should consider them as separate entities.
If you want the "Client_Authentication" webapp to communicate with the "Server_Authentication" webapp you need to POST the information from the "Client_Authentication" to the "Server_Authentication" and process the response back in the "Client_Authentication" webapp.
A very basic example:
In the "Client_Authentication" webapp, you have a button called "Submit". In the view you add a script like this
$("submit").click(function(){
$.post("YOUR_HOSTNAME_OF_SERVER_AUTHENTICATION", function(data, status){
alert("Response: " + data);
});
});
This will output the response from "Server_Authentication" in an alertbox.
Please note that the above is a very basic example with the script embedded in the view. You might want to consider separating the scripts from the view, but this is outside of the scope of the question

Using MVC5 and Web API 2 in the same project

I'm about to start a new project in ASP.NET MVC5, which has a bit of Web API too. I'll also need a Windows Forms client which will call the API. This client has a file system watcher that detects when a file has been changed, and will post the contents to the API.
When the API receives the data, it does some calculations, and ideally will send the results through SignalR to the browser and update the display.
I'm getting rather stuck trying to work out the authentication. I want to use Individual User Accounts, so the user can log in with the Windows Forms client (and get a token) and in the browser to view the data.
I've got as far as File -> New -> Project, and tried an MVC project with the Web API box checked, and a Web API with the MVC box checked. Looking at the two AccountController classes that these generate, they seem quite different.
I guess the options are
Try to get these two controllers working together
Call the MVC controller from the Windows Forms client
Have two projects in the solution and try to work out how to use SignalR to talk between them.
A better way?
I suspect the last one. I've not used Web API before, so I could be doing this all wrong. What approach should I take?
I would say, create 2 different projects, 1 for MVC 1 for API.
Use 1 BLL which is referenced in both of them and carries the logic for both of them and will not be dealing with separate controllers.
Of course if you need other layers like DataAccess or Repository, you have to create them once and they will be referenced in the BLL which is later referenced in both MVC and API interfaces.

Which application is better to develop for the following requirement

I need to store data received from a remote device, i.e Application needs to run 24 hours so that it can capture data and store it in database.
I am confusing whether to create a console application, web application or any other i need to develop which will run continuously.
If you already have the application developed, then send the data to a web service. if you don't, consider creating a web page, either using ASP.Net or something like JQuery Mobile and push data to HTTP Handlers or Web Service.
If you go down the web service route, create a web method that accepts a request object and returns a response object. It should be a pretty simple design.

Passing C# class to javascript (no web server)

I am writing a desktop application in C# and would like to pass data from C# to an HTML 5 panel (javascript or jquery). I am using CefSharp which embeds Chromium into the application, it successfully loads data from a file with no web server, I have this up and running to display Processing sketches locally within the application (desktop).
The solution I am looking for can't use or require a web server, it has to operate locally. The HTML 5 (CefSharp) browser panel is hosted by the application and does not allow users to navigate to web pages. Its only purpose is to visualise and display data the software generates.
Ideally I would like to do something like the following:
Data in C# ----> (possibly) JSON object ----> object available in embedded browser
The browser has an instance of jQuery if that helps. I'd be grateful if you a small code sample can be provided, it has to operate without a web server and preferably without dumping the C# data to a file which is then loaded by the embedded browser. I want to pass data directly from C# to the embedded browser and have it available for processing.
Any ideas?
References:
https://github.com/chillitom/CefSharp

How a web app identify if a click came from another web app via code?

we have a web application that users can take online reports from ou ERP system data... And we have another web application that is used by our teachers and employees.
We can't change the ERP web app because its a closed DLL, in this case we made some extended functionality in our custom internal web app and we are willing to put this functionality on the "menu" of the ERP web app.
I need to integrate the two applications in the following way:
When I click in the menu of the ERP web app, I want that our internal web app assert that the click have come from our ERP web app and not typed in the URL, this is possible?
Consider the ServerVariable called HTTP_REFERER below is a link to some documentation
http://www.w3schools.com/asp/coll_servervariables.asp
Look for the server variable HTTP_REFERER in your internal app's request. You should be able to compare that against a known value.

Categories