You can tell which controller is serving the view, by looking at the code, and also the url in asp.net. is there any other way to tell which controller is creating the view besides the two way i mentioned?
Asked
Active
Viewed 53 times
0
-
Instead of asking this question, could you explain where you need to know this and why? – CodeCaster Sep 18 '14 at 06:38
-
possible duplicate of [Get controller and action name from within controller?](http://stackoverflow.com/questions/18248547/get-controller-and-action-name-from-within-controller) – CodeCaster Sep 18 '14 at 06:40
-
http://stackoverflow.com/questions/14462751/in-mvc3-how-to-get-the-current-controller-name – CodeCaster Sep 18 '14 at 06:40
2 Answers
0
View depends on Actions in Controller. For example if you have a controller and the Action like below,
public class HelloWorldController : Controller
{
public ActionResult Index()
{
return View();
}
and in the Views/HelloWorld folder a file Index.cshtml then you can say Index.cshtml view is for Index action of HelloWolrd Controller. It works as follows,
- There would be subfolder with the name of the Controller in the Views Folder
- There would be actionname.cshtml file representing view of a particular action
Hope it helps

Debug_mode
- 80
- 6
0
A nice, short snippet, to fetch the controller name in the view, is this one:
@ViewContext.RouteData.Values["controller"]
Just use it in your view, where needed.

Tobias
- 2,811
- 2
- 20
- 31