在超过一定长度的剃刀页面中使用路由参数导致无效 url 错误
Using route parameter in razor pages over a certain length causing invalid url error
我正在尝试使用名为 {error} 的路由参数将错误消息传递到我在 razor 中的错误页面。但是我一直收到无效的 url 错误。我把它归结为我作为路由参数传递的 url 参数的长度,这导致了这个问题。
但是我不明白这个,因为url的有效长度是2048个字符。而且字符串的长度明显更长。任何见解将不胜感激。
这是一个 returns 无效的 url 错误。总字符数:404,url参数:373
http:///localhost123/Error/An%20error%20occurred%20while%20updating%20the%20entries.%20See%20the%20inner%20exception%20for%20details.The%20INSERT%20statement%20conflicted%20with %20the%20FOREIGN%20KEY%20constraint%20"FK_VthaFormsWorkflow_VthaForms".%20The%20conflict%20occurred%20in%20database%20"MyDWPApps",%20table%20"dbo.VthaForms",%20column%20 %27Id%27.%20The%20statement%20has%20been%20terminated./True
如果我删除最后一句,url 有效。总字符数:359,url参数:328
http:///localhost123/Error/An%20error%20occurred%20while%20updating%20the%20entries.%20See%20the%20inner%20exception%20for%20details.The%20INSERT%20statement%20conflicted%20with %20the%20FOREIGN%20KEY%20constraint%20"FK_VthaFormsWorkflow_VthaForms".%20The%20conflict%20occurred%20in%20database%20"MyDWPApps",%20table%20"dbo.VthaForms",%20column%20 %27Id%27./真
//Error.cshtml
@page "{error}/{hidenavbar:bool=false}"
@model ErrorModel
@{
ViewData["Title"] = "Error";
if (Model.HideNavbar)
{
Layout = "_LayoutHideNavbar";
}
}
<div class="header-title text-center">
<h1 class="title">Error</h1>
</div>
<div class="card w-75 text-center mx-auto p-4">
<div><b>Time of Error:</b> @DateTime.Now.ToShortTimeString()</div>
<div class="text-danger"><b>Error message:</b> @Model.Error</div>
<br />
</div>
//Error.cshtml.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
namespace VehicleTakeHomeApp.Pages
{
public class ErrorModel : PageModel
{
[BindProperty(SupportsGet = true)]
public bool HideNavbar { get; set; }
[BindProperty(SupportsGet = true)]
public string Error { get; set; }
private readonly ILogger<ErrorModel> _logger;
public ErrorModel(ILogger<ErrorModel> logger)
{
_logger = logger;
}
public void OnGet()
{
}
}
}
将最终使用 nlog 记录器并将所有内部异常消息传输到 nlog 记录器输出到文件。根据@Steve 的建议,所有用户错误都将显示一条通用错误消息。
我正在尝试使用名为 {error} 的路由参数将错误消息传递到我在 razor 中的错误页面。但是我一直收到无效的 url 错误。我把它归结为我作为路由参数传递的 url 参数的长度,这导致了这个问题。
但是我不明白这个,因为url的有效长度是2048个字符。而且字符串的长度明显更长。任何见解将不胜感激。
这是一个 returns 无效的 url 错误。总字符数:404,url参数:373
http:///localhost123/Error/An%20error%20occurred%20while%20updating%20the%20entries.%20See%20the%20inner%20exception%20for%20details.The%20INSERT%20statement%20conflicted%20with %20the%20FOREIGN%20KEY%20constraint%20"FK_VthaFormsWorkflow_VthaForms".%20The%20conflict%20occurred%20in%20database%20"MyDWPApps",%20table%20"dbo.VthaForms",%20column%20 %27Id%27.%20The%20statement%20has%20been%20terminated./True
如果我删除最后一句,url 有效。总字符数:359,url参数:328
http:///localhost123/Error/An%20error%20occurred%20while%20updating%20the%20entries.%20See%20the%20inner%20exception%20for%20details.The%20INSERT%20statement%20conflicted%20with %20the%20FOREIGN%20KEY%20constraint%20"FK_VthaFormsWorkflow_VthaForms".%20The%20conflict%20occurred%20in%20database%20"MyDWPApps",%20table%20"dbo.VthaForms",%20column%20 %27Id%27./真
//Error.cshtml
@page "{error}/{hidenavbar:bool=false}"
@model ErrorModel
@{
ViewData["Title"] = "Error";
if (Model.HideNavbar)
{
Layout = "_LayoutHideNavbar";
}
}
<div class="header-title text-center">
<h1 class="title">Error</h1>
</div>
<div class="card w-75 text-center mx-auto p-4">
<div><b>Time of Error:</b> @DateTime.Now.ToShortTimeString()</div>
<div class="text-danger"><b>Error message:</b> @Model.Error</div>
<br />
</div>
//Error.cshtml.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
namespace VehicleTakeHomeApp.Pages
{
public class ErrorModel : PageModel
{
[BindProperty(SupportsGet = true)]
public bool HideNavbar { get; set; }
[BindProperty(SupportsGet = true)]
public string Error { get; set; }
private readonly ILogger<ErrorModel> _logger;
public ErrorModel(ILogger<ErrorModel> logger)
{
_logger = logger;
}
public void OnGet()
{
}
}
}
将最终使用 nlog 记录器并将所有内部异常消息传输到 nlog 记录器输出到文件。根据@Steve 的建议,所有用户错误都将显示一条通用错误消息。