CS0012 C# 在未引用的程序集中定义了类型 'HttpContext'

CS0012 C# The type 'HttpContext' is defined in an assembly that is not referenced

我有使用 Spire 将文档合并到 .PDF 的代码,但我在这一行中得到以下代码 result.Save(outputFile,Spire.Pdf.FileFormat.PDF);

CS0012 C# The type 'HttpContext' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

我试图将此 Assembly 代码添加到 App.config 文件,但没有成功。

<assemblies>
        <add assembly="MyAssembly" Version="4.0.0.0" Culture="neutral" PublicKeyToken="b03f5f7f11d50a3a"/>   

下面是代码

private void button1_Click(object sender, EventArgs e)
        {

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "All files (*.docx, *.pdf, *.pptx, *.pdf)| *.docx; *.pdf; *.pptx; *.xlsx";

            ofd.Multiselect = true;

            if (DialogResult.OK == ofd.ShowDialog())
            {

                string[] files = ofd.FileNames;

                listBox1.Items.AddRange(files);
            }

    }

        private void button2_Click(object sender, EventArgs e)
        {
            string ext = string.Empty;
            List<Stream> filesStreams = new List<Stream>();
            MemoryStream ms1 = new MemoryStream();
            MemoryStream ms2 = new MemoryStream();
            MemoryStream ms3 = new MemoryStream();
            foreach (object item in listBox1.Items)
            {
                ext = Path.GetExtension(item.ToString());
                switch (ext)
                {
                    case ".docx":
                        Document doc = new Document(item.ToString());
                        doc.SaveToStream(ms1, Spire.Doc.FileFormat.PDF);
                        filesStreams.Add(ms1);

                        break;
                    case ".pdf":
                        filesStreams.Add(File.OpenRead(item.ToString()));
                        break;
                    case ".pptx":
                        Presentation ppt = new Presentation(item.ToString(), Spire.Presentation.FileFormat.Auto);
                        ppt.SaveToFile(ms2, Spire.Presentation.FileFormat.PDF);
                        filesStreams.Add(ms2);

                        break;
                    case ".xlsx":
                        Workbook xls = new Workbook();
                        xls.LoadFromFile(item.ToString());
                        xls.SaveToStream(ms3, Spire.Xls.FileFormat.PDF);
                        filesStreams.Add(ms3);

                        break;
                    default:
                        break;

                }
            }
            string outputFile = "result.doc";
            PdfDocumentBase result = PdfDocument.MergeFiles(filesStreams.ToArray());
            //result.SaveToDoc(outputFile);
            result.Save(outputFile,Spire.Pdf.FileFormat.PDF);
            ms1.Close();
            ms2.Close();
            ms3.Close();

    }

谢谢

通过在解决方案资源管理器 window 中右键单击项目,然后从上下文菜单中单击“添加引用”选项,添加对 System.Web 库的引用。

以上操作将打开“添加引用”对话框,您需要从 .Net 选项卡中选择 System.Web 库并单击“确定”。