[英]List all views in ASP.MVC
是否可以列出項目中的所有視圖及其相對路徑? 無需手動靜態輸入所有視圖。
我環顧了 Razor 命名空間和類以及 ViewEngine 命名空間和類,但我自己看不到任何東西。
這可能是一個老問題,但這里是。
選項1
您需要從Global.asax
文件中檢索程序集名稱。 命名空間和第一個公共類應該結合起來得到下面的程序集名稱。
namespace XYZ
{
public class MvcApplication : System.Web.HttpApplication
程序集是XYZ.MvcApplication
下面的查詢將檢索 ASP.NET MVC 項目中所有控制器和操作方法的列表。 下面的代碼將幫助您將其枚舉到視圖中。
@using System.Reflection;
@{
ViewBag.Title = "Dashboard";
Assembly asm = Assembly.GetAssembly(typeof(XYZ.MvcApplication));
var paths = asm.GetTypes()
.Where(type => typeof(System.Web.Mvc.Controller).IsAssignableFrom(type))
.SelectMany(type => type.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public))
.Where(m => !m.GetCustomAttributes(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute), true).Any())
.Select(x => new { Controller = x.DeclaringType.Name.Replace("Controller", null), Method = x.Name })
.OrderBy(x => x.Controller).ThenBy(x => x.Method).ToList();
}
<table class="table table-bordered table-condensed table-striped">
<thead>
<tr>
<th>Controller</th>
<th>Method</th>
</tr>
</thead>
<tbody>
@foreach (var i in paths)
{
<tr>
<td>@i.Controller</td>
<td>@i.Method</td>
</tr>
}
</tbody>
</table>
選項 2
您的非技術同事還有另一種選擇來獲取您的視圖中使用的文件列表。
使用名為Freeplane的免費軟件獲得以下結果。
請使用文件>>導入>>文件夾結構功能來獲取您需要的特定視圖。
然后可以使用 File >> Export Map 將這些導出為 Excel 文件
如果您只想查看所有視圖列表,只需檢查 ~/Views 目錄 (Server.MapPath) 下的文件。 我認為沒有必要反思。
使用以下鏈接獲取所有視圖,而不使用它們的相對路徑:
例如代替
@Html.ActionLink("Delete Dinner", "Delete", "Dinners", new { id = Model.DinnerID }, null)
T4MVC讓您編寫
@Html.ActionLink("Delete Dinner", MVC.Dinners.Delete(Model.DinnerID))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.