0

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

9
  • This question has nothing to do with opening an EXE. It's entirely related to routing and your controller method. Commented Jan 9, 2019 at 5:46
  • @John , I might not have clearly explained in the question. But I want to open a notepad via Web API. Is it possible?
    – CrazyCoder
    Commented Jan 9, 2019 at 5:47
  • 3
    "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: The requested resource does not support http method 'GET'." - indicates that there is an issue with routing, or that you need the [HttpGet] attribute. It doesn't matter what your controller method does, the fact is that ASP.NET doesn't know how to route the request to your controller. Therefore it's nothing to do with opening an EXE. Commented Jan 9, 2019 at 5:48
  • 1
    If you wish to open exe on the client machine while your API running on Server, it's not possible.
    – Chetan
    Commented Jan 9, 2019 at 5:49
  • @ChetanRanpariya , No , I want to open the exe on server where API is hosted
    – CrazyCoder
    Commented Jan 9, 2019 at 5:50

1 Answer 1

1

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.