本地主机错误 - 找不到此页面 - Asp.net Core MVC
Localhost Error - This page can't be found - Asp.net Core MVC
我正在尝试将 HTML 内容转换为 pdf 文件。当我点击打印收据按钮时,它应该显示收据的 pdf 文件。相反,它给出 无法找到此页面 错误。
这是按钮的代码:
<form method="post" asp-action="ReceiptAsync">
<div class="col-md-2">
<button type="submit" class="btn btn-primary btn-lg p-2" id="generatePdf">Print Receipt</button>
</div>
</form>
控制器方法的代码如下:
public async Task<IActionResult> ReceiptAsync()
{
using (var stringWriter = new StringWriter())
{
var viewResult = _compositeViewEngine.FindView(ControllerContext, "ViewReceipt", false);
if (viewResult == null)
{
throw new ArgumentNullException("View Can't Be Found");
}
var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());
var viewContext = new ViewContext(
ControllerContext,
viewResult.View,
viewDictionary,
TempData,
stringWriter,
new HtmlHelperOptions()
);
await viewResult.View.RenderAsync(viewContext);
var htmlToPdf = new HtmlToPdf(1000, 1414);
htmlToPdf.Options.DrawBackground = true;
var pdf = htmlToPdf.ConvertHtmlString(stringWriter.ToString());
var pdfbytes = pdf.Save();
return File(pdfbytes, "application/pdf");
}
}
错误截图如下:
ASP.NET 核心 3.0 之前:
- 可以在
Payment/ReceiptAsync
访问前面的操作
路线.
- Link 生成需要指定
Async
后缀。
ASP.NET Core 3.0 之后:
- 可以在
Payment/Receipt
路由访问前面的操作。
- Link生成不需要指定
Async
后缀。
因此您需要将代码更改为:
<form method="post" asp-action="Receipt"> //change `asp-action` here...
<div class="col-md-2">
<button type="submit" class="btn btn-primary btn-lg p-2" id="generatePdf">Print Receipt</button>
</div>
</form>
参考:
[Discussion] Async suffix for controller action names will be trimmed by default
我正在尝试将 HTML 内容转换为 pdf 文件。当我点击打印收据按钮时,它应该显示收据的 pdf 文件。相反,它给出 无法找到此页面 错误。
这是按钮的代码:
<form method="post" asp-action="ReceiptAsync">
<div class="col-md-2">
<button type="submit" class="btn btn-primary btn-lg p-2" id="generatePdf">Print Receipt</button>
</div>
</form>
控制器方法的代码如下:
public async Task<IActionResult> ReceiptAsync()
{
using (var stringWriter = new StringWriter())
{
var viewResult = _compositeViewEngine.FindView(ControllerContext, "ViewReceipt", false);
if (viewResult == null)
{
throw new ArgumentNullException("View Can't Be Found");
}
var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());
var viewContext = new ViewContext(
ControllerContext,
viewResult.View,
viewDictionary,
TempData,
stringWriter,
new HtmlHelperOptions()
);
await viewResult.View.RenderAsync(viewContext);
var htmlToPdf = new HtmlToPdf(1000, 1414);
htmlToPdf.Options.DrawBackground = true;
var pdf = htmlToPdf.ConvertHtmlString(stringWriter.ToString());
var pdfbytes = pdf.Save();
return File(pdfbytes, "application/pdf");
}
}
错误截图如下:
ASP.NET 核心 3.0 之前:
- 可以在
Payment/ReceiptAsync
访问前面的操作 路线. - Link 生成需要指定
Async
后缀。
ASP.NET Core 3.0 之后:
- 可以在
Payment/Receipt
路由访问前面的操作。 - Link生成不需要指定
Async
后缀。
因此您需要将代码更改为:
<form method="post" asp-action="Receipt"> //change `asp-action` here...
<div class="col-md-2">
<button type="submit" class="btn btn-primary btn-lg p-2" id="generatePdf">Print Receipt</button>
</div>
</form>
参考:
[Discussion] Async suffix for controller action names will be trimmed by default