How can I add a function that all Razor Pages can access?
Asked Answered
C

2

8

Using Razor Pages ASP.Net Core, I have some functions that I'd like to use on every page. I guess this used to be done with App_Code, but that no longer seems to work in Core. How can I accomplish this in Asp.Net Core Razor Pages?

Columbus answered 10/2, 2020 at 21:29 Comment(1)
would Service Injection work for you? learn.microsoft.com/en-us/aspnet/core/mvc/views/…Kimsey
C
17

Option 1 - DI

1 - Create the service class with the relevant functionality

public class FullNameService
{
    public string GetFullName(string first, string last)
    {
        return $"{first} {last}";
    }
}

2- Register the service in startup

services.AddTransient<FullNameService>();

3- Inject it to the razor page

public class IndexModel : PageModel
{
    private readonly FullNameService _service;

    public IndexModel(FullNameService service)
    {
        _service = service;
    }

    public string OnGet(string name, string lastName)
    {
        return _service.GetFullName(name, lastName);
    }
}

Option 2 - Base Model

1- Create a base page model with the function

public class BasePageModel : PageModel
{
    public string GetFullName(string first, string lastName)
    {
        return $"{first} {lastName}";
    }
}

2- Derive other pages from the base model

public class IndexModel : BasePageModel
{
    public string OnGet(string first, string lastName)
    {
        return GetFullName(first, lastName);
    }
}

Option 3 - Static Class

1- Use a static function that can be accessed from all pages

public static class FullNameBuilder
{
    public static string GetFullName(string first, string lastName)
    {
        return $"{first} {lastName}";
    }
}

2- Call the static function from the razor page

public class IndexModel : PageModel
{
    public string OnGet(string first, string lastName)
    {
        return FullNameBuilder.GetFullName(first, lastName);
    }
}

Option 4 - Extension Methods

1- Create an extension method for a specific type of objects (e.g. string)

public static class FullNameExtensions
{
    public static string GetFullName(this string first, string lastName)
    {
        return $"{first} {lastName}";
    }
}

2- Call the extension from razor page

public class IndexModel : PageModel
{
    public string OnGet(string first, string lastName)
    {
        return first.GetFullName(lastName);
    }
}
Cocteau answered 11/2, 2020 at 5:14 Comment(0)
E
0

I'd add one more solution

Option 5 - inheritance

  1. Define generic class which inherits from RazorPage<TModel>

    public abstract class MyBasePage : RazorPage

  2. Make all view inheriting from it in _ViewImports.cshtml

    @inherits MyBasePage

  3. Define methods (an other members) in the class (protected accessibility is enough) an use them from CSHTML

e.g.

definition

protected string? PageClass { get => ViewData["PageClass"] as string; set => ViewData["PageClass"] = value; }

usage

@{
    Title = "Error";
}

or

<head>
    <title>@Title</title>
Eloquence answered 3/3, 2024 at 10:52 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.