In _Layout.cshtml
is it possible to determine what View is going to be rendered in @RenderBody()
?
You can get the View (i.e. Index.cshtml) through ((RazorView)ViewContext.View).ViewPath
Example for your needs:
<script type="text/javascript" src="~/Content/Scripts/@(Path.GetFileNameWithoutExtension(Server.MapPath(((RazorView)ViewContext.View).ViewPath))).js"></script>
If you need your actual View (i.e. _Layout.cshtml), you can use VirtualPath
instead.
Old answer Reading your comments, you want to add a
<script ...>...</script>
depending on the view but outside of @RenderBody()
?
Then put
@RenderSection("Scripts", required:false)
and in your view define the section like
@section Scripts {
<script ...>...</script>
}
So you don't need to maintain your _Layout.cshtml since every View defines their own scripts.
Here is an easy explanation: http://weblogs.asp.net/scottgu/asp-net-mvc-3-layouts-and-sections-with-razor
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With