-2

What I am trying to do is, add a button on my nav bar if the user is on a certain page (view).

I set my nav bar in my _Layout.cshtml

<ul class="nav navbar-nav">
    <li><a asp-controller="Home" asp-action="Index" class="navbar-brand">bethany's Pie Shop</a></li>
    <li><a asp-controller="Feedback" asp-action="Index">Feedback</a></li>
</ul>

So something like @IfUser is on Details view, add this list item.

Other answers don't seem to work in .netcore 2.0

Metzer
  • 211
  • 1
  • 6
  • 20
  • https://stackoverflow.com/questions/1268763/retrieve-the-current-view-name-in-asp-net-mvc you could have a look at this question. – Qpirate Apr 09 '18 at 07:54
  • Possible duplicate of [Retrieve the current view name in ASP.NET MVC?](https://stackoverflow.com/questions/1268763/retrieve-the-current-view-name-in-asp-net-mvc) – Qpirate Apr 09 '18 at 07:54
  • 1
    In my humble opinion, it is not a duplicate, because it is an ASP.NET core quetsion, because the question doesn't care about passing the viewname to the view – Emmanuel DURIN Apr 09 '18 at 08:38

1 Answers1

3

In the Razor view, just access :

@this.Path

This will provide Something like :

~/Views/MailBox/Index.cshtml

If the code is in a layout file, to access the page, just write :

@this.ViewContext.View.Path

Then you can compare with a given view easily

Emmanuel DURIN
  • 4,803
  • 2
  • 28
  • 53