S6798 — [JSInvokable] attribute should only be used on public methods

Language
C#
Type
Bug
Severity
Major
Tags
blazor

Why is this an issue?

In Blazor, the [JSInvokable] attribute is used to annotate a method, enabling it to be invoked from JavaScript code. The prerequisite for this functionality is that the method must be declared as public.
Otherwise, a runtime error will be triggered when an attempt is made to call the method from JavaScript.

How to fix it

To fix the issue, ensure the methods annotated with the [JSInvokable] attribute are public.

Code examples

Noncompliant code example


@code {
    [JSInvokable]
    private static void MyStaticMethod() { } // Noncompliant

    [JSInvokable]
    internal void MyMethod() { } // Noncompliant
}

Compliant solution


@code {
    [JSInvokable]
    public static void MyStaticMethod() { } // Compliant

    [JSInvokable]
    public void MyMethod() { } // Compliant
}

Resources

Documentation

↑ Back to top