會出現此問題主因是null值所致的問題,改善方法是將日期型態的資料增加null值的處理即可改善此問題。
Google Code Prettify
Fix error: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles.
In order to fix the error:
System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles.
You have at least two options:
Avoiding circular references
Ignoring circular references
I’ll assume that option 1 (which is usually ideal) it’s not viable for you.
Option 2 is literally a little statement in the Startup class, in the ConfigureServices method (this is for ASP.NET Core 6+):
services.AddControllers().AddJsonOptions(x =>
x.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);
If you use ASP.NET Core 5:
services.AddControllers().AddJsonOptions(x =>
x.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve);
If you are using ASP.NET Core 3.1 or less, you can do the following:
Install the library: Microsoft.AspNetCore.Mvc.NewtonsoftJson (install the corresponding version, if you have ASP.NET Core 3.1, you install version 3.1 of the library)
Then, in ConfigureServices:
services.AddControllers().AddNewtonsoftJson(x =>
x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
修復不同版本的 ASP.NET Core 中“檢測到可能的對象循環”錯誤
為了修復錯誤:
System.Text.Json.JsonException: 檢測到可能的對象循環。這可能是由於循環或對象深度大於最大允許深度 32。考慮在 JsonSerializerOptions 上使用 ReferenceHandler.Preserve 以支持循環。
你至少有兩個選擇:
避免循環引用
忽略循環引用
我會假設選項 1(通常是理想的)對您來說不可行。
選項 2 實際上是 Startup 類中的一個小聲明,在 ConfigureServices 方法中(這是針對ASP.NET Core 6+):
services.AddControllers().AddJsonOptions(x =>
x.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);
如果您使用ASP.NET Core 5:
services.AddControllers().AddJsonOptions(x =>
x.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve);
如果您使用的是ASP.NET Core 3.1 或更低版本,則可以執行以下操作:
安裝庫:Microsoft.AspNetCore.Mvc.NewtonsoftJson(安裝對應版本,如果有ASP.NET Core 3.1,則安裝3.1版本的庫)
然後,在配置服務中:
services.AddControllers().AddNewtonsoftJson(x =>
x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
異常的改善: 指定的路徑、檔名,或是兩者都太長。完整的檔名必須少於 260 個字元,並且目錄名稱必須少於 248 個字元
在 VS2019 中發行網站時出現錯誤 "指定的路徑、檔名,或是兩者都太長。完整的檔名必須少於 260 個字元,並且目錄名稱必須少於 248 個字元。", 造成發佈失敗。
解決方法:
修改環境變數 TEMP 及 TMP 為 C:\TEMP ,獲得改善。
不一定要用路徑 C:\TEMP, 但是要儘量的簡短。
iTextsharp barcode example
protected void submitPdf_Click(object sender, EventArgs e) { BarCodeGenerate(); } ////// Generates the new page with barcode128. /// public void BarCodeGenerate() { Document doc = new Document(PageSize.A4.Rotate(), 50, 50, 20, 20); PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(@"c:\temp\BarCodeQRCode.pdf", FileMode.Create)); doc.Open(); PdfContentByte cb = writer.DirectContent; PdfContentByte cb39 = writer.DirectContent; Paragraph pa = new Paragraph(); Barcode128 barcode = new Barcode128(); barcode.CodeType = Barcode.CODE128_UCC; barcode.Code = "hello barcode - code128"; iTextSharp.text.Image barcodeImage = barcode.CreateImageWithBarcode(cb, null, null); barcodeImage.ScalePercent(100f); barcodeImage.Alignment = Element.ALIGN_CENTER; pa.Add(barcodeImage); BaseFont fontChinese = BaseFont.CreateFont("C:/Windows/Fonts/STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font normalFont = new Font(fontChinese, 12); //float[] w = { 5f, 15f, 6f, 10f }; float[] w = { Convert.ToSingle(PageSize.A4.Width * 0.4), Convert.ToSingle(PageSize.A4.Width * 0.3), Convert.ToSingle(PageSize.A4.Width * 0.3) }; PdfPTable table = new PdfPTable(w); table.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER; table.DefaultCell.BorderWidth = 0; table.DefaultCell.BorderColor = new BaseColor(0, 0, 0); table.DefaultCell.Padding = 4; table.DefaultCell.SpaceCharRatio = 4; table.WidthPercentage = 100; table.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT; PdfPCell cell = new PdfPCell(new Phrase("Create a table with all default settings")); cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER; cell.HorizontalAlignment = Rectangle.ALIGN_CENTER; table.AddCell(cell); var aa = new Paragraph("aaa", normalFont); cell = new PdfPCell(new Phrase("Create a table 中文字的測試with all default settings", normalFont)); cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER; cell.HorizontalAlignment = Rectangle.ALIGN_CENTER; table.AddCell(cell); cell = new PdfPCell(new Phrase("Create a table with all default settings")); cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER; cell.HorizontalAlignment = Rectangle.ALIGN_CENTER; table.AddCell(cell); cell = new PdfPCell(new Phrase("Create a table with all default settings")); cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER; cell.HorizontalAlignment = Rectangle.ALIGN_CENTER; table.AddCell(cell); doc.Add(table); float[] w2 = { Convert.ToSingle(PageSize.A4.Width * 0.4), Convert.ToSingle(PageSize.A4.Width * 0.2), Convert.ToSingle(PageSize.A4.Width * 0.1), Convert.ToSingle(PageSize.A4.Width * 0.3) }; //float[] w2 = { 25f, 15f, 30f, 10f }; table = new PdfPTable(w2); table.WidthPercentage = 100; cell = new PdfPCell(new Phrase("Create a table with all default settings")); cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER; cell.HorizontalAlignment = Rectangle.ALIGN_CENTER; //cell.FixedHeight = 10f; table.AddCell(cell); cell = new PdfPCell(barcodeImage, true); cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER; cell.HorizontalAlignment = Rectangle.ALIGN_CENTER; cell.Padding = 5f; //cell.FixedHeight = 10f; table.AddCell(cell); cell = new PdfPCell(new Phrase("Create a table with all default settings")); cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER; cell.HorizontalAlignment = Rectangle.ALIGN_CENTER; //cell.FixedHeight = 10f; table.AddCell(cell); cell = new PdfPCell(new Phrase("Create a table with all default settings")); cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER; cell.HorizontalAlignment = Rectangle.ALIGN_CENTER; //cell.FixedHeight = 10f; table.AddCell(cell); doc.Add(table); Barcode39 code39 = new Barcode39(); code39.Code = "314159265"; //code39.CodeType = Barcode.CODE128_UCC; code39.StartStopText = true; code39.GenerateChecksum = true; code39.Extended = true; iTextSharp.text.Image code39Image = code39.CreateImageWithBarcode(cb39, null, null); //iTextSharp.text.Image code39Image = code39.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White); pa.Add(code39Image); cell = new PdfPCell(new Phrase("Create a table with all default settings")); cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER; cell.HorizontalAlignment = Rectangle.ALIGN_CENTER; //cell.FixedHeight = 10f; table.AddCell(cell); cell = new PdfPCell(code39Image, true); cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER; cell.HorizontalAlignment = Rectangle.ALIGN_CENTER; cell.Padding = 5f; //cell.FixedHeight = 10f; table.AddCell(cell); cell = new PdfPCell(new Phrase("Create a table with all default settings")); cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER; cell.HorizontalAlignment = Rectangle.ALIGN_CENTER; //cell.FixedHeight = 10f; table.AddCell(cell); cell = new PdfPCell(new Phrase("Create a table with all default settings")); cell.Border = Rectangle.TOP_BORDER + Rectangle.BOTTOM_BORDER + Rectangle.LEFT_BORDER + Rectangle.RIGHT_BORDER; cell.HorizontalAlignment = Rectangle.ALIGN_CENTER; //cell.FixedHeight = 10f; table.AddCell(cell); table.AddCell(cell); doc.Add(table); table.AddCell(cell); doc.Add(table); table.AddCell(cell); doc.Add(table); code39.StartStopText = true; code39.GenerateChecksum = false; code39.Extended = true; code39Image = code39.CreateImageWithBarcode(cb39, null, null); pa.Add(code39Image); table.AddCell(cell); doc.Add(table); table.AddCell(cell); doc.Add(table); table.AddCell(cell); doc.Add(table); BarcodeQRCode qrcode = new BarcodeQRCode("hello barcode - QrCode", 100, 100, null); iTextSharp.text.Image grImage = qrcode.GetImage(); pa.Add(grImage); /* https://dotblogs.com.tw/kevinya/2016/01/13/104010 由於 iTextSharp 內建的 QRCode 類別對於中文內容的支援度較差, 需經過幾道 override 的程序改寫才可存中文,所以在此直接採用 第三方元件 MessagingToolkit.QRCode。 利用此第三方元件的QRCodeEncoder物件即可產生QRCode的點陣圖Bitmap, 再將bitmap轉換成iTextSharp.text.Image即可加入到pdf之中: QRCodeEncoder chtEncoder = new QRCodeEncoder(); //第三方先產生bitmap Bitmap qrBitmap1 = chtEncoder.Encode("qrcode裝中文內容"); //將內容轉碼成 QR code //再將bitmap轉換成iTextSharp.text.Image iTextSharp.text.Image qrImage1 = iTextSharp.text.Image.GetInstance(qrBitmap1, BaseColor.BLACK); qrImage1.ScalePercent(50); qrImage1.Alignment = Element.ALIGN_RIGHT;//透過Image.Alignment來設定靠左靠右 pa.Add(qrImage1); doc.Add(pa); doc.Close(); */ doc.Add(pa); doc.Close(); }