1

I am running a windows forms application on a server which do many things. My intention is to get some web functionality to the application, for example view the logfile in a browser or manipulate the Settings.Defaults via browser.

Is it possible to add the web functionality to a winforms application ?

5
  • 1
    What do you mean by view the logfile in a browser using a winform application? Commented Jan 14, 2016 at 6:30
  • You can use WebBrowserContorl in windows forms. Commented Jan 14, 2016 at 6:40
  • <h2>to get some web functionality to the application</h2><br/>I think you need to explain more about that however you can search for http listener but instead of it i rather suggest you to build a web application which could share content coming from your winform app. Commented Jan 14, 2016 at 6:48
  • No WebBrowserControl is for using a Webbrowser in a windows form, what i mean is to access the winform app from a browser outside the server. For example I contact via browser from a client pc the url servername/winformapp.aspx or something to get the mentioned features. @Shaharyar: On this page I would see what the app on the server is doing. At the moment it writes to a local textfile. Commented Jan 14, 2016 at 6:49
  • You will have to make a web service which will read that file and return the data to you. You can also achieve it by making a simple webform and throw data using Response.Write. But web service is preferable. Commented Jan 14, 2016 at 6:57

1 Answer 1

2

Yes, you can by self hosting web API into your windows form application like demonstrated here

The second option is to use ASP.NET 5 self-hosting but it is still in beta and has no good documentation yet.

Edit 1: To support viewing pages using Web API 2 you need to follow the following steps: First, let JSON formatter handle HTML requests like the following

config.Routes.MapHttpRoute(
  "API Default", "api/{controller}/{id}",
  new { id = RouteParameter.Optional });

// Add the supported media type.            
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

Second, Save the desired html template files on disk with placeholders that will be replaced with your actual data at runtime and change the controller like below

    public String GetAllProducts()
    {
        //Should be loaded from HTML template file ex. Products.html
        string html = "<html><body><table border=\"2\"><tr><th>ID</th><th>Name</th><th>Category</th><th>Price</th><tr>{{placeholder}}</table></body></html>";
        StringBuilder sb = new StringBuilder();
        foreach (var item in products)
        {
            sb.AppendLine("<tr><td>" + item.Id + "</td><td>" + item.Name + "</td><td>" + item.Category + "</td><td>" + item.Price + "</td></tr>");
        }
        return html.Replace("{{placeholder}}", sb.ToString());
    }

Note: If you don't like the default JSON formatter you can write your own like there did tn this project

Sign up to request clarification or add additional context in comments.

2 Comments

The link sounds interesting I will have a look at that.
Hi, the link helps me much, bit I used the owin method. I'm now able to get for example the Setting.Default.sourcepath value by localhost:9000/api/settings. But how give I the app a web interface ? I mean, going to localhost:9000/index.aspx (or html?) where I define all my Settings for example which post it to my api ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.