Freelance Project Requests info@CodeDocu.de Software Development in C# WPF Asp.Net Core Vba Excel Word SQL-Server EF Linq, UWP Net
#

 

 

How can I find the current caller of an Asp.Net Core MVC page directly in the view?

If I am in the general layout view, and I want to set the current controller as the path in the navigation bar, then I need the information at runtime, which page was passed and from which controller, action and ID the website was loaded as a view.

 

Solution:

In Asp-MVC View you can read out the current controller with

ViewContext.RouteData.Values["controller"]

 

 

In the debugger in the breakpoint you can see that the ViewContext -> RouteData provides two System.Collections.

In the System.Collection Values ​​you can query the list by key-value pair.

For the controller enter ViewContext.RouteData.Value ["controller"] and get "Note"

 

Watch 1 extract in the hold position in the view shown

-                   Values          {Microsoft.AspNetCore.Routing.RouteValueDictionary}       
+                  Comparer     {System.OrdinalIgnoreCaseComparer}            Count 2        int

-                   Keys   {string[2]}          System.Collections.Generic.ICollection<string> {string[]}

                    [0]      "controller"    string

                    [1]      "action"         string

-                   Values          {object[2]}          System.Collections.Generic.ICollection<object> {object[]}

                    [0]      "Note" object {string}

                    [1]      "Index"         object {string}

 

 

 

 

 

example

Customize navigation bar

The navigation bar in the standard Asp.Net Core MVC application is built like this.

Now it would be useful to convert them so that the current controller area would be marked as active

<a  asp-controller="Note" asp-action="Index" class="mynav_a_bg50">Notizen</a>

    <a href="#news" class="mynav_a_bg50">Groups</a>

    <a href="#news" class="mynav_a_bg50">Videos</a>

    <a href="#news" class="mynav_a_bg50">Kleinanzeigen</a>

    &nbsp;

 

New Razor code

@{

    string sController = ViewContext.RouteData.Values["controller"] as string;

}

 

<span style="color:red ">Controller=@sController</span>

@if (sController == "Note")

{

    <a asp-controller="Note" asp-action="Index" style="background-color:red">Notizen</a>

}

 

else

{

    <a asp-controller="Note" asp-action="Index" class="mynav_a_bg50">Notizen</a>

}

<a href="#news" class="mynav_a_bg50">Groups</a>

<a href="#news" class="mynav_a_bg50">Videos</a>

<a href="#news" class="mynav_a_bg50">Kleinanzeigen</a>

&nbsp;

<a href="#news">MyStuff <img src="~/images/Ico/icoSwitch_On.png" style="padding:0;margin:0px;" /></a>

 

 

 

Microsoft.AspNetCore.Routing.RouteValueDictionary

System.Collections.Generic.IEqualityComparer <string> {System.OrdinalIgnoreCaseComparer}

 

 

Mobile
»
Asp MVC: Determine current controllers and action in a view
»
Asp.Net MVC: How to Define the Parent Layout Element?

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de