同一解決方案中有 2 個專案。第一個專案是一個 .NET Core 專案,它包含與包相關的所有代碼(控制器、模型等)。我需要獲取包的資訊(id、名稱、描述)并將其顯示在第二個專案(.NET Core Web App with Razor)中。是否可以在不更改第一個專案的情況下做到這一點?我只想在單個網頁上顯示包串列。我嘗試呼叫第一個專案的控制器,但沒有成功。也許我錯過了一點。任何幫助表示贊賞。
uj5u.com熱心網友回復:
這個要求是可以實作的,請看下面的gif圖。
尖端
如果你想從同一個解決方案的一個專案中呼叫另一個專案的控制器,你需要確保兩個專案中都有 HomeController。我的意思是任何類的名稱在兩個專案中都應該是唯一的。
否則,您將面臨與我的主頁相同的問題。

測驗代碼:
public List<PackageReference> GetPackageList5(string projectname)
{
List<PackageReference> list = new List<PackageReference>();
PackageReference p = null;
XDocument doc = XDocument.Load(_webHostEnvironment.ContentRootPath "/" projectname ".csproj");
var packageReferences = doc.XPathSelectElements("//PackageReference")
.Select(pr => new PackageReference
{
Include = pr.Attribute("Include").Value,
Version = pr.Attribute("Version").Value
});
Console.WriteLine($"Project file contains {packageReferences.Count()} package references:");
foreach (var packageReference in packageReferences)
{
p = new PackageReference();
p.Version= packageReference.Version;
p.Include= packageReference.Include;
list.Add(packageReference);
//Console.WriteLine($"{packageReference.Include}, version {packageReference.Version}");
}
return list;
}
我的測驗步驟:
創建兩個專案
Net5MVC,,Net6MVC
添加專案參考。



我的 .net6 專案參考了一個 .net5 專案。所以在我的 HomeController (.net) 中,我在下面添加:

using Net5MVC.ForCore6; using Net5MVC.Models;
建議
當我們在.net6專案中參考.net5專案時,我們可以構建成功,但是當我們部署它時,它總是失敗。原因是某些檔案是
multiple publish output files with the same relative path.Found multiple publish output files with the same relative path: D:\..\Net6\Net6\Net5MVC\appsettings.Development.json, D:\..\Net6\Net6\Net6MVC\appsettings.Development.json, D:\..\Net6\Net6\Net5MVC\appsettings.json, D:\..\Net6\Net6\Net6MVC\appsettings.json.并且通常會將類別庫添加到當前專案,而不是添加 Web 專案。
我們知道我們可以在 .csproj 檔案中找到包資訊,所以我們需要復制并粘貼 .csproj 檔案來發布檔案夾。
我仍然建議使用上面的 GetPackageList5 方法作為您專案的介面,使用 HttpClient 進行請求。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/476888.html
標籤:asp.net-mvc asp.net 核心 .net-core asp.net-web-api 剃刀页面
上一篇:如果提交的空日期默認為默認值,為什么ModelState無效?
下一篇:將輸入值傳遞給asp-route
