2

Is it possible to see the MVC View structure from within a browser? As in, is it possible to enable some configuration that will show you the file name of each View and Partial view being rendered from within a browser?

i.e.

If I had Index.cshtml, which then renders two partial views Content1.cshtml and Content2.cshtml, then I would like to see the file names from the F12 browser tool:

//Index.cshtml
<html> .....
....
//Content1.cshtml
<table> ... </table>
//Content2.cshtml
<div> ... </div>

</html>
goamn
  • 1,939
  • 2
  • 23
  • 39
  • Use BrowserLink – SLaks Feb 02 '17 at 23:44
  • @SLaks And then ... ? I can see this inside my F12: ... where am I supposed to be looking? – goamn Feb 03 '17 at 00:09
  • http://vswebessentials.com/features/browserlink – SLaks Feb 03 '17 at 03:24
  • @SLaks I like this approach but it doesn't work for me ... we are using knockout and it is complaining with an error in the browserlink.js file: ``Uncaught Error: Syntax error, unrecognized expression: input[data-bind='value: Vendor.Agent.Name, attr: {name: 'request.Vendor.Agent.Name'}, valueUpdate: ['click', 'change', 'input']'] – goamn Feb 06 '17 at 00:22

1 Answers1

2

You can use one of the Caller Info attributes.

Create new ViewFileName function:

using System.IO;
using System.Web.Mvc;
using System.Runtime.CompilerServices;

public static class Extensions
{
    public static IHtmlString ViewFileName([CallerFilePath]string filePath = "unknown")
    {
        return new MvcHtmlString("<!-- " + Path.GetFileName(filePath) + " -->");
    }
}

Then call it in every regular and partial view:

@Extensions.ViewFileName()

For more information check Caller Information (C#) documentation, Creating Custom HTML Helpers (C#) article by Microsoft ASP.NET Team and Print the source filename and linenumber in C# question.

Community
  • 1
  • 1
Leonid Vasilev
  • 11,910
  • 4
  • 36
  • 50
  • This works but it's not what I'm looking for, if there are 20 partial views and I have to place code inside all of them, then I will most likely find the place that I want to find and I would not be saving any time ... I can usually always find the place but I wanted to make the process faster. – goamn Feb 06 '17 at 00:24
  • 1
    Do you use `Html.RenderPartial` to render partial views? – Leonid Vasilev Feb 06 '17 at 06:35
  • Yes, the majority of times. – goamn Feb 07 '17 at 01:14