2

I am beginner in software field. I have one ASP.Net MVC 5 (c#) project. In that I need to write one end point which can be called from a desktop app. Basically desktop app needs to send one integer value to my controller action aka end point. I know how to create a web api. So, I added a WebApi controller in my controller folder namely "HelloController" and below is the code.

    public IHttpActionResult Follow(int y)
    {
       // some code
        return Ok();
    }

Question 1. Is that it? So, in my desktop app can I call now /HelloController/Follow/34

Question 2. Or Am I totally wrong. And I need some other end point and not the WebApi end point. Please guide me here.

P.S: My desktop app is in VB.Net

Unbreakable
  • 7,776
  • 24
  • 90
  • 171

1 Answers1

2

That is pretty much it. You'll want to enable Cors. This answer has some good information on setting Cors in ASP.NET MVC 5. There are two main ways to set CORS, the first way in the linked answer(setting the response header HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", "*");) or in the web.config with:

Sorry, my mistake, Cors isn't required from desktop apps to api's. I'm used to setting Cors for web apps.

With the Web Api endpoint, you should be able to consume that from pretty much anything that can create a Web request. I don't specifically know VB.Net or I'd give you an example.

An example web request in C# would look something like this:

var url = someUrl;

using (var client = new HttpClient())
{
    var response = await client.GetAsync(url);

    var responseString = await response.Content.ReadAsStringAsync();
    var deserializedData = JsonConvert.DeserializeObject<MyModel>(responseString);
}

Create an HttpClient, perform a Get request asynchronously. The response string is serialized JSON, so it must be deserialized to a usable model.

That's a basic example, but hope it helps.

Here is a website that contains VB.net examples for REST.

Community
  • 1
  • 1
Tyler Jennings
  • 8,761
  • 2
  • 44
  • 39