anthonypliu
anthonypliu

Reputation: 12437

How do I add custom tags in the head of my razor views

I want to add custom javascript in the head of certain views. I have the normal structuring, with a shared folder that has a view called _Layout.cshtml. This file has the head tag in it and is the basic "master page" for all my views. I want a certain view to have certain javascript in the head tag, but have everything else the same, how do I accomplish this?

Upvotes: 2

Views: 579

Answers (1)

nathan gonzalez
nathan gonzalez

Reputation: 11987

check out the @RenderSection method. you pass it a name and whether the section is required (yours would be false), and then in your views you set that section up using @Section.

to sum up, put something like this in your layout:

@RenderSection("OptionalContent", required: false)

and something like this in your view:

@section ExtraContent {
    <div>Some extra content</div>
}

Upvotes: 2

Related Questions