简体   繁体   English

C#函数在开发机上可用,但在Web上失败

[英]C# Function works on development machine but fails on web

I have this function in my Visual Studio web app and it seems to work just fine when I run the project on the development machine, but when I publish it to the web and go to the page where the function runs, it always runs the catch exception part. 我在Visual Studio Web应用程序中有此功能,当我在开发计算机上运行项目时,它似乎工作得很好,但是当我将其发布到Web上并转到该函数运行的页面时,它将始终运行catch例外部分。 Can anyone help? 有人可以帮忙吗? I'm scratching my head here :( Here is the code for the function. 我在这里挠头:(这是该函数的代码。

public void printData()
{
    try
    {
        //The path to the Bid Form
        string pdfPath = Server.MapPath("PDF/Bid_Form.pdf");
        var pdfReader = new PdfReader(pdfPath);
        var pdfOutput = new MemoryStream();
        var pdfStamper = new PdfStamper(pdfReader, pdfOutput);
        CultureInfo ci = new CultureInfo("en-US");

        //This code block gets the date
        string date = BidDateTextBox.Value;
        DateTime dateNow = Convert.ToDateTime(date);
        string newDate = dateNow.ToString("MMMM dd, yyyy", ci);

        //This gets the values from the hidden fields
        decimal airTotal = (Convert.ToDecimal(at.Value) + Convert.ToDecimal(att.Value));
        decimal waterTotal = (Convert.ToDecimal(wt.Value) + Convert.ToDecimal(wtt.Value));
        decimal preTestAir = String.IsNullOrEmpty(AirPreTestTextBox.Value) ? 0 : decimal.Parse(AirPreTestTextBox.Value);
        decimal preTestWater = String.IsNullOrEmpty(WaterPreTestTextBox.Value) ? 0 : decimal.Parse(WaterPreTestTextBox.Value);
        decimal preTestCombine = preTestAir + preTestWater;
        decimal airTotalAll = (Convert.ToDecimal(at.Value) + Convert.ToDecimal(att.Value) + preTestAir);
        decimal waterTotalAll = (Convert.ToDecimal(wt.Value) + Convert.ToDecimal(wtt.Value) + preTestWater);

        //This line gets the total of all areas
        decimal combineTotal = Convert.ToDecimal(gt.Value);

        //This line gets the checked check boxes in the Per Specifications area
        string selectedSpecItems = (String.Join(",", CheckBoxList1.Items.OfType<ListItem>().Where(r => r.Selected).Select(r => r.Text)) + ", " + CustomSpecTextBox.Value);

        //This line gets the checked check boxes in the Exclusions area
        string selectedExItems = (String.Join(",", CheckBoxList2.Items.OfType<ListItem>().Where(r => r.Selected).Select(r => r.Text)) + ", " + CustomExcTextBox.Value);

        //This checks if the pre test check box is checked. If so it doesnt include the pretest amounts.
        if (PreTestCheckBox.Checked) {
            pdfStamper.AcroFields.SetField("PreTestAirBid", "$" + preTestAir);
            pdfStamper.AcroFields.SetField("PreTestWaterBid", "$" + preTestWater);
            pdfStamper.AcroFields.SetField("PreTestCombineBid", "$" + preTestCombine);
            pdfStamper.AcroFields.SetField("PreTestText", "Pre-Test");
            pdfStamper.AcroFields.SetField("AirBid", "$" + airTotal);
            pdfStamper.AcroFields.SetField("WaterBid", "$" + waterTotal);
            pdfStamper.AcroFields.SetField("CombineBid", "$" + combineTotal);
        }
        else
        {
            pdfStamper.AcroFields.SetField("PreTestAirBid", "");
            pdfStamper.AcroFields.SetField("PreTestWaterBid", "");
            pdfStamper.AcroFields.SetField("PreTestCombineBid", "");
            pdfStamper.AcroFields.SetField("AirBid", "$" + airTotalAll);
            pdfStamper.AcroFields.SetField("WaterBid", "$" + waterTotalAll);
            pdfStamper.AcroFields.SetField("CombineBid", "$" + combineTotal);

        }
        pdfStamper.AcroFields.SetField("Date", "" + newDate);
        pdfStamper.AcroFields.SetField("Bid#", "");
        pdfStamper.AcroFields.SetField("Location", "" + LocationTextBox.Value);
        pdfStamper.AcroFields.SetField("ProjectName", "" + ProjectNameTextBox.Value);
        pdfStamper.AcroFields.SetField("Engineer", "" + EngineerTextBox.Value);
        pdfStamper.AcroFields.SetField("EngineerPhone", "");
        pdfStamper.AcroFields.SetField("EngineerFax", "");
        pdfStamper.AcroFields.SetField("Architect", "" + ArchitectTextBox.Value);
        pdfStamper.AcroFields.SetField("ArchitectPhone", "");
        pdfStamper.AcroFields.SetField("ArchitectFax", "");
        pdfStamper.AcroFields.SetField("Specifications", "" + selectedSpecItems);
        pdfStamper.AcroFields.SetField("Exclusions", "" + selectedExItems);
        pdfStamper.FormFlattening = false;
        pdfStamper.Close();
        pdfReader.Close();
        Response.AddHeader("Content-Disposition", "attachment; filename=NewBid.pdf");
        Response.ContentType = "application/pdf";
        Response.BinaryWrite(pdfOutput.ToArray());
        Response.End();
    }
    catch (Exception e)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "ErrorAlert", "alert('An error has occured.');", true);
    }

}

Without knowing the exception my guess would be the server does not have appropriate access to the file at the path you are referencing. 在不知道异常的情况下,我猜测服务器将无法正确访问您所引用路径中的文件。 Try outputting the exception message in the catch block instead of your custom error message. 尝试在catch块中输出异常消息,而不是您的自定义错误消息。 Should help. 应该有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c# 应用程序在开发机器上工作,在非开发机器上失败 - c# application working on development machine, fails on non-development machine 从本地计算机使用时,调用SQL存储过程的C#函数可以工作,但是从云中的Azure函数调用时,失败 - C# function that calls a SQL stored procedure works when used from local machine but fails when called from an Azure function in the cloud C#Web API可在本地计算机上运行,​​但不能在服务器上运行 - C# Web API works on local machine but not on server 旧的ASP.NET 4应用程序的生成服务器无法生成新的C#7,但是在开发中可以正常工作 - Build server of an old ASP.NET 4 application fails building new C# 7, but it works in development 在电子邮件中发送忘记的用户密码在本地机器上成功运行,但在使用 c# asp.net 的服务器上失败 - Sending forgot password of user in an email works successfully in local machine but fails on server using c# asp.net 在非开发机器上使用C#类库 - Using C# Class Library On Non-Development Machine 查询在HeidiSQL中有效,但在C#中失败 - Query works in HeidiSQL but fails in C# 与Arduino交谈:在C#中工作,在Python中失败 - Talking to an Arduino: works in C#, fails in Python 在C#中将数据发布到网页失败 - posting data to web pages fails in C# 使用C#创建Web共享失败,没有错误 - Create web share with C# fails with no errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM