【问题标题】:Is it possible to see the MVC View structure from within a browser?是否可以从浏览器中查看 MVC 视图结构?
【发布时间】:2017-06-20 05:49:57
【问题描述】:

是否可以从浏览器中查看 MVC 视图结构? 例如,是否可以启用一些配置来向您显示从浏览器中呈现的每个视图和部分视图的文件名?

如果我有 Index.cshtml,然后会呈现两个部分视图 Content1.cshtml 和 Content2.cshtml,那么我想从 F12 浏览器工具中查看文件名:

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

</html>

【问题讨论】:

  • 使用 BrowserLink
  • @SLaks 然后……?我可以在 F12 中看到这个: ...我应该去哪里看?
  • @SLaks 我喜欢这种方法,但它对我不起作用......我们正在使用淘汰赛,它在 browserlink.js 文件中抱怨错误:``未捕获的错误:语法错误, 无法识别的表达式:input[data-bind='value: Vendor.Agent.Name, attr: {name: 'request.Vendor.Agent.Name'}, valueUpdate: ['click', 'change', 'input'] ']

标签: asp.net-mvc google-chrome razor browser partial-views


【解决方案1】:

您可以使用呼叫者信息属性之一。

新建ViewFileName函数:

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) + " -->");
    }
}

然后在每个常规和部分视图中调用它:

@Extensions.ViewFileName()

有关更多信息,请查看 Microsoft ASP.NET 团队的 Caller Information (C#) documentationCreating Custom HTML Helpers (C#) articlePrint the source filename and linenumber in C# question

【讨论】:

  • 这行得通,但这不是我要找的,如果有 20 个部分视图并且我必须在所有部分中放置代码,那么我很可能会找到我想要找到的地方而且我不会节省任何时间......我通常总能找到那个地方,但我想让这个过程更快。
  • 你使用Html.RenderPartial来渲染局部视图吗?
  • 是的,大多数时候。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-03
  • 2015-11-06
  • 1970-01-01
相关资源
最近更新 更多