<h3><%= User.Identity.IsAuthenticated ? true : User.Identity.Name; %></h3>
我想寫一個單行代碼,當成員已經登錄時回傳成員的姓名。但是 IsAuthenticated 代碼已經回傳 true,所以我需要使用 true 嗎?另外,除非用戶未登錄,否則我不想做任何事情。在這種情況下。當您想在一行中撰寫簡潔的代碼時。哪個代碼最好?
uj5u.com熱心網友回復:
您可以使用具有以下形式的條件運算子:
condition ? consequent : alternative
在您的情況下,這將是:
<h3><%= User.Identity.IsAuthenticated ? User.Identity.Name : string.Empty; %></h3>
uj5u.com熱心網友回復:
你可以用這個 <h3>@(User.Identity.IsAuthenticated ? User.Identity.Name :"")</h3>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/395815.html
上一篇:讀取Azure中托管的ASP.NETCore中的Excel檔案
下一篇:我想從父表中洗掉值,但出現例外
