Search code examples
c#asp.net-mvcasp.net-web-apiclass-library

How to open exe using WebAPI and class library


I'm creating WebAPI in c# .NET framework 4.6.1. I have taken an empty template, added a controller:

 public class InfoController : ApiController
{
    public bool LaunchNotePad()
    {
        AppServices as = new AppServices();
        bool result = as.LaunchNotePad();
        return result;
    }
    
}

I have a class library in the same solution which has some methods like below:

public class AppServices
    {
        System.Diagnostics.Process.Start();
        public bool LaunchApplication()
        {
            bool result = false;
            Process.Start("notepad.exe", "SomeName");
            return true;

        }
    }

In my WebApiConfig.cs:

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );  

When I host this and run the URL http://IPAddress/api/Info/LaunchNotePad, I expect this to actually open the notepad in the server and return true, but it doesn't. Instead it is showing some error like this:

<Error>
<Message>
The requested resource does not support http method 'GET'.
</Message>
</Error>

In short, I want to call a class library from API which can access the exe files on the server. In this example, I have shown notepad as sample, but in reality it is a exe which we have created. So every time I send the request, it has to open the notepad in the server where it is hosted.

Is this possible? What am I missing in this?

I have added a picture to show what I'm trying to achieve:

enter image description here


Solution

  • As this is an API Controller it will support the methods GET, POST, PUT, DELETE by default. Add the [HttpGet] attribute to your LaunchNotePad method or change the name of your method to GetLaunchNotePad.