Razor Pages and MVC and CodeBehind Together
Tags: csharp, dotnet, beginners, tutorial
Published: 2024-02-05 18:33:20

## 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](https://github.com/elanatframework/Code_behind). 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](https://elanat.net/page_content/code_behind) 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.

```csharp
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();
```

### 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.

```csharp
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();
```

### 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.

```csharp
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();
```

### 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.

```csharp
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();
```

### 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](https://github.com/elanatframework/Code_behind/blob/elanat_framework/doc/how_to_use_code_behind.md)

[CodeBehind on GitHub](https://github.com/elanatframework/Code_behind)

[CodeBehind in NuGet](https://www.nuget.org/packages/CodeBehind/)

[CodeBehind Website](https://elanat.net/page_content/code_behind)