c# - Determine what View is going to be rendered in @RenderBody() -


in _layout.cshtml possible determine view going rendered in @renderbody() ?

you can view (i.e. index.cshtml) through ((razorview)viewcontext.view).viewpath

example needs:

<script type="text/javascript" src="~/content/scripts/@(path.getfilenamewithoutextension(server.mappath(((razorview)viewcontext.view).viewpath))).js"></script> 

if need actual view (i.e. _layout.cshtml), can use virtualpath instead.


old answer reading comments, want add

<script ...>...</script> 

depending on view outside of @renderbody()?

then put

@rendersection("scripts", required:false) 

and in view define section like

@section scripts {    <script ...>...</script> } 

so don't need maintain _layout.cshtml since every view defines own scripts.

here easy explanation: http://weblogs.asp.net/scottgu/asp-net-mvc-3-layouts-and-sections-with-razor


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -