Asp.net mvc 将Html转换为Pdf时身份验证失败错误

Asp.net mvc 将Html转换为Pdf时身份验证失败错误,asp.net-mvc,asp.net-mvc-5,html-to-pdf,Asp.net Mvc,Asp.net Mvc 5,Html To Pdf,我尝试使用“HtmlToPdf”nuget将Html转换为PDF,在本地测试中效果很好,但当我将站点上载到主机时,出现以下错误: Conversion error: Authentication error. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information

我尝试使用“HtmlToPdf”nuget将Html转换为PDF,在本地测试中效果很好,但当我将站点上载到主机时,出现以下错误:

 Conversion error: Authentication error.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Exception: Conversion error: Authentication error.
这是我的转换方法代码

        [AllowAnonymous]
        public ActionResult Convert(int id)
        {

            HtmlToPdf converter = new HtmlToPdf();
            var context = System.Web.HttpContext.Current;
            string baseUrl = context.Request.Url.Host + ":"+context.Request.Url.Port + "/Doctor/DietTherapy/LineRegimePrint/";
            PdfDocument doc = converter.ConvertUrl(baseUrl + id);

            // save pdf document
            byte[] pdf = doc.Save();

            // close pdf document
            doc.Close();

            // return resulted pdf document
            FileResult fileResult = new FileContentResult(pdf, "application/pdf");
            fileResult.FileDownloadName = "Document.pdf";
            return fileResult;
        }

如何授权用户进行此转换?

听起来您只需要使用PDF库即可。例如,如果它使用基本HTTP身份验证:

HtmlToPdf converter = new HtmlToPdf();
converter.Options.Authentication.Username = "some username";
converter.Options.Authentication.Password = "some password";
// the rest of your code...

链接文档还包含其他身份验证方法的示例。

您是否检查了这行代码的值是否正确(在主机服务器上运行时)


什么是
HtmlToPdf
class?你调试过了吗?您知道错误实际发生在哪一行吗?您正在查找此错误吗?:@trailmax HtmlToPdf是用于将html页面转换为PDF的nuget模块,正如我稍后所说的,该模块在本地计算机中工作正常,只是我在将项目上载到主机时遇到问题,我想我应该在身份验证方面做些什么@大卫,这似乎解决了我的问题!等等,我查一下it@David错误已更改为“/”应用程序中的
服务器错误。转换错误:无法打开url感谢您的回答,我无法解决我在上一条评论中所说的问题。从库中抛出编辑。
转换错误:无法打开url。
更多:
在执行当前web请求期间生成了未经处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息。
i使用直接链接更改了该行,但出现了相同的错误:
string baseUrl=”http://www.mehrdiet.com/Doctor/DietTherapy/LineRegimePrint/";             PdfDocument doc=converter.ConvertUrl(baseUrl+id)我的上一个错误:异常详细信息:系统。异常:转换错误:无法打开url。源错误:
在执行当前web请求期间生成了未处理的异常。有关异常的起源和位置的信息可以使用下面的异常堆栈跟踪来识别你确定吗?代码中的冒号“:”会发生什么变化?调用.Port字符串baseUrl=context.Request.Url.Host+“:“+context.Request.Url.Port+”/Doctor/dietherapy/LineRegimePrint/”;我没有看到您所说的“:”(来自“context.Request.Url.Host+”:“+context.Request.Url.Port”)中的“:”。根据代码的外观,它应该返回类似“”的内容
string baseUrl = context.Request.Url.Host + ":"+context.Request.Url.Port + "/Doctor/DietTherapy/LineRegimePrint/";