Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added using statement to the Razor Helper usage example #17040

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/docs/reference/core/Razor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ Many extensions methods are available in Razor with `@Orchard`.
If you want to use an extension method in a view, you can inject an `IOrchardHelper` named `Orchard` at the top of your file:

```csharp
@inject OrchardCore.IOrchardHelper Orchard
// You can also put this into a _ViewImports.cshtml file so it's applied to all templates.
@using OrchardCore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is right if it's not in the _ViewImports.cshtml

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not there by default.

@inject IOrchardHelper Orchard
Comment on lines +48 to +49
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already injected in OC, if you are using OrchardCore.DisplayManagement.Razor.RazorPage from within _ViewImports

public IOrchardDisplayHelper Orchard
{
get
{
if (_orchardHelper == null)
{
EnsureDisplayHelper();
_orchardHelper = new OrchardDisplayHelper(Context, _displayHelper);
}
return _orchardHelper;
}
}

```

In `OrchardCore.DisplayManagement.Razor`, there is a RazorPage that already has a public property `Orchard` that you can use to call an extension method or the current `HttpContext`.

If you want to use an Orchard helper in a controller, you can inject an instance in the constructor:

```csharp
using OrchardCore;
private IOrchardHelper _orchard;

public MyClass(IOrchardHelper orchard)
Expand Down