DEV Community

Cover image for How to Load Controller Class From Another DLL?
elanatframework
elanatframework

Posted on

How to Load Controller Class From Another DLL?

In this tutorial, we are going to invoke a Controller class from another DLL in a main project. The calling method is completely modular and the DLL is not added to the main project.

This tutorial is related to a project that is created based on ASP.NET Core.

When the modular name comes in ASP.NET Core, we have to use the CodeBehind framework. If you think the default MVC in ASP.NET Core has this modular feature, please leave a comment in the comments section.

In this tutorial, we will first create a main project and compile it and run it on the web server. Then we create a module project and put its DLL in wwwroot/bin path in the main project.

Steps to create the main project

Step 1: First, in Visual Studio, we create a new empty project under ASP.NET Core version 7.0.

Step 2: We install the latest version of CodeBehind framework through NuGet packages.

Step 3: Configure the Program.cs class as follows.



var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

SetCodeBehind.CodeBehindCompiler.Initialization();

app.Run(async context =>
{
    CodeBehind.CodeBehindExecute execute = new CodeBehind.CodeBehindExecute();
    await context.Response.WriteAsync(execute.RunRoute(context, 0));
});

app.Run();


Enter fullscreen mode Exit fullscreen mode

The code above is the Route configuration to implement the controller class

Step 4: Create new CodeBehind Controller class and put main in the name of the Controller class.

CodeBehind Controller



using CodeBehind;

namespace YourProjectName
{
    public partial class main : CodeBehindController
    {
        public void PageLoad(HttpContext context)
        {
            MainModel model = new MainModel();
            model.Value = "My text in main project";
            View("/main.aspx", model);
        }
    }
}


Enter fullscreen mode Exit fullscreen mode

According to the Controller class above, if the path example.com/main is requested, the Controller class above is executed and the main.aspx page is initialized with a Model value named MainModel and then called.

Note : Because we want the Controller class codes to be displayed in the Program.cs class after the Route configuration, we teach how to create a View and Model below.

Step 5: Create new Model class as follows.

Model class



namespace YourProjectName
{
    public class MainModel
    {
        public string Value { get; set; }
    }
}


Enter fullscreen mode Exit fullscreen mode

Step 6: Create new View file as follows.

View (main.aspx)



@page
@model {YourProjectName.MyModel}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Main project</title>
</head>
<body>
    @model.Value
</body>
</html>


Enter fullscreen mode Exit fullscreen mode

Step 7: Run the project and then request the path example.com/main to see the result.

Result for example.com/main request



<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Main project</title>
</head>
<body>
    My value text in main project
</body>
</html>


Enter fullscreen mode Exit fullscreen mode

Note: Please compile the project and put it on the web server to have a better understanding of the modularity of the CodeBehind framework.

Steps to create a module project

Step 1: First, in Visual Studio, we create a new empty project under ASP.NET Core version 7.0.

Step 2: We install the latest version of CodeBehind framework through NuGet packages.

Step 3: Configure the Program.cs class as follows.



var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

app.Run();


Enter fullscreen mode Exit fullscreen mode

Step 4: Create new CodeBehind Controller class and put module in the name of the Controller class.

CodeBehind Controller



using CodeBehind;

namespace YourProjectName
{
    public partial class module : CodeBehindController
    {
        public void PageLoad(HttpContext context)
        {
            ModuleModel model = new ModuleModel();
            model.Value = "My text in module project";
            View("/module.aspx", model);
        }
    }
}


Enter fullscreen mode Exit fullscreen mode

According to the Controller class above, if the path example.com/module is requested, the Controller class above is executed and the module.aspx page is initialized with a Model value named ModuleModel and then called.

Step 5: Create new Model class as follows.

Model class



namespace YourProjectName
{
    public class ModuleModel
    {
        public string Value { get; set; }
    }
}


Enter fullscreen mode Exit fullscreen mode

Step 6: Create new View file as follows.

View (module.aspx)



@page
@model {YourProjectName.ModuleModel}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Module project</title>
</head>
<body>
    @model.Value
</body>
</html>


Enter fullscreen mode Exit fullscreen mode

Step 7: Publish the module project.

Steps to add the module project to the main project

Step 1: Copy the module.aspx file from the module project to wwwroot directory in the main project.

Step 2: We copy the DLL file of the module project to wwwroot/bin path in the main project.

Modularity in the CodeBehind framework

Step 3: We run the main project and then we request the example.com/module path.

Result for example.com/module request



<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Module project</title>
</head>
<body>
    My text in module project
</body>
</html>


Enter fullscreen mode Exit fullscreen mode

As you can see, we introduced the modular structure of the powerful CodeBehind framework in practice.

If you have any information about creating a modular project in the default ASP.NET Core structure, please share it with us and other users.

Related links

CodeBehind on GitHub:
https://github.com/elanatframework/Code_behind

CodeBehind in NuGet:
https://www.nuget.org/packages/CodeBehind/

CodeBehind page:
https://elanat.net/page_content/code_behind

Top comments (0)