DEV Community

Cover image for Razor Pages and MVC and CodeBehind Together
elanatframework
elanatframework

Posted on

Razor Pages and MVC and CodeBehind Together

Introduction

If we ignore the Blazor technology, you can use MVC and Razor Pages to create systems under .NET Core. Of course, you can also use the powerful CodeBehind framework. In this article, we teach how to configure ASP.NET Core frameworks, including MVC, Razor pages, and CodeBehind together in the Program.cs class.

It is interesting to know that the CodeBehind framework works up to 40% faster than MVC and Razor Pages. We recommend that you check out the CodeBehind framework and compare it to MVC and Razor Pages in ASP.NET Core.

You can install the CodeBehind framework through NuGet in the following link in a web project under ASP.NET Core:
https://www.nuget.org/packages/CodeBehind/

Note: All examples are related to .NET Core version 7.0.

Razor Pages and MVC config in Program.cs

The codes below are configuration for Razor and MVC pages. Using this configuration allows you to use Razor pages and MVC in ASP.NET Core at the same time.

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();

var app = builder.Build();

app.UseRouting();

app.MapControllerRoute(
    name: "default",
    pattern: "MVC/{controller=Home}/{action=Index}");

app.MapRazorPages();

app.Run();
Enter fullscreen mode Exit fullscreen mode

Razor Pages and CodeBehind config in Program.cs

The codes below are configuration for Razor pages and CodeBehind framework. Using this configuration allows you to use Razor pages and CodeBehind in ASP.NET Core at the same time.

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();

var app = builder.Build();

app.MapRazorPages();

SetCodeBehind.CodeBehindCompiler.Initialization();

app.Use(async (context, next) =>
{
    CodeBehind.CodeBehindExecute execute = new CodeBehind.CodeBehindExecute();

    string PageResult = execute.Run(context);

    if (execute.FoundPage)
        await context.Response.WriteAsync(PageResult);
    else
        await next();
});

app.Run();
Enter fullscreen mode Exit fullscreen mode

MVC and CodeBehind config in Program.cs

The code below is the configuration for MVC and the CodeBehind framework. Applying this configuration allows you to use the default MVC and CodeBehind in ASP.NET Core at the same time.

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllersWithViews();

var app = builder.Build();

app.UseRouting();

app.MapControllerRoute(
    name: "default",
    pattern: "MVC/{controller=Home}/{action=Index}");

SetCodeBehind.CodeBehindCompiler.Initialization();

app.Use(async (context, next) =>
{
    CodeBehind.CodeBehindExecute execute = new CodeBehind.CodeBehindExecute();

    string PageResult = execute.Run(context);

    if (execute.FoundPage)
        await context.Response.WriteAsync(PageResult);
    else
        await next();
});

app.Run();
Enter fullscreen mode Exit fullscreen mode

Razor Pages and MVC and CodeBehind config in Program.cs

The codes below are a super config! MVC and Razor Pages and CodeBehind work side by side without interference.

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();

var app = builder.Build();

app.UseRouting();

app.MapControllerRoute(
    name: "default",
    pattern: "MVC/{controller=Home}/{action=Index}");

app.MapRazorPages();

SetCodeBehind.CodeBehindCompiler.Initialization();

app.Use(async (context, next) =>
{
    CodeBehind.CodeBehindExecute execute = new CodeBehind.CodeBehindExecute();

    string PageResult = execute.Run(context);

    if (execute.FoundPage)
        await context.Response.WriteAsync(PageResult);
    else
        await next();
});

app.Run();
Enter fullscreen mode Exit fullscreen mode

What do we get from configuring CodeBehind alongside Razor Pages and MVC?

The CodeBehind framework is superior to the default ASP.NET Core frameworks. CodeBehind is modular, fast, simple, modern, understandable, gives you output and has advanced features.

Related links

How to Use CodeBehind framework

CodeBehind on GitHub

CodeBehind in NuGet

CodeBehind Website

Top comments (0)