How to self host an existing asp net webform

Introduction

Self-hosting an existing ASP.NET Web application can be a useful approach when you want to run your application of the traditional IIS hosting environment. This article will guide you through the process of self-hosting an existing ASP.NET Web Forms application with examples.

Prerequisites

Before we begin, make sure you have the following prerequisites:

Step 1: Create a Console Application

The first step is to create a new console application in Visual Studio. This will serve as the host for your ASP.NET Web Forms application.


using Microsoft.Owin.Hosting;
using System;

namespace SelfHostedWebForms
{
    class Program
    {
        static void Main(string[] args)
        {
            string baseAddress = "http://localhost:8080/";

            using (WebApp.Start(url: baseAddress))
            {
                Console.WriteLine("Server running at {0}", baseAddress);
                Console.ReadLine();
            }
        }
    }
}

Step 2: Configure OWIN Startup

Next, you need to configure the OWIN startup class to host your ASP.NET Web Forms application.


 .Owin;
using Owin;
using System.Web.;

[assembly: OwinStartup(typeof(SelfHostedWebForms.Startup))]

 SelfHostedWebForms
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    }
}

Step 3: Register Routes

In this step, you need to register the routes for your ASP.NET Web Forms application. This is similar to how you would configure routes in a traditional ASP.NET Web Forms application.


using System.Web.Routing;

namespace SelfHostedWebForms
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.MapPageRoute("Default", "", "~/Default.aspx");
        }
    }
}

Step 4: Run the Application

Now that you have set up the console application and configured the OWIN startup and routes, you can run the application.


using Microsoft.Owin.Hosting;
using System;

namespace SelfHostedWebForms
{
    class Program
    {
        static void Main(string[] args)
        {
            string baseAddress = "http://localhost:8080/";

            using (WebApp.Start(url: baseAddress))
            {
                Console.WriteLine("Server running at {0}", baseAddress);
                Console.ReadLine();
            }
        }
    }
}

Conclusion

By following the steps outlined in this article, you should now have a self-hosted ASP.NET Web Forms application up and running. This approach allows you to run your application outside of the traditional IIS hosting environment, giving you more flexibility and over your application's deployment.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents