Separate .NET Core Razor C # features in all views

I know that there is a way for ad C # functions inside the view and call them using the method @functions{ ... }inside my view, but is there a way to create a general view with these functions to include controllers inside the view without copying the same line of code on each of them ? I tried using @injectother methods inside the view _Layout, but obviously these methods cannot be called. I also tried to create an external class like this, but I want to use views only if this is possible:

public class Functions : RazorPage<dynamic>
{
    public override Task ExecuteAsync()
    {
        throw new NotImplementedException();
    }

    public string GetTabActive(string lang)
    {
        if (ViewBag.lang.ToString() == lang) return "active";
        return "";
    }
}
+4
source share
2 answers

- , _ViewImports, , :

@inject Functions func

StartUp , :

services.AddSingleton<Functions>();

, :

<h2>@func.MyFunction</h2>
0

, WebViewPage

public abstract class TestView<TViewModel> : WebViewPage<TViewModel>
{
    public void TestMethod()
    {
    }
}

"Inherits"

@inherits TestView<dynamic>

@TestMethod()

-

@model @inherits .

+2

Source: https://habr.com/ru/post/1687019/


All Articles