问题描述
我正在尝试使用名为{error}的路由参数将错误消息传递到razor中的错误页面。但是我一直收到无效的网址错误。我把它归结为要作为导致此问题的路由参数传递的url参数的长度。
但是我不明白这一点,因为URL的有效长度是2048个字符。字符串的长度明显更高。任何见识将不胜感激。
这将返回无效的URL错误。总字符数:404,URL参数:373
http:// localhost123 / Error /发生%20error%20%20,而%20更新%20the%20entries。%20See%20the%20inner%20exception%20for%20details。%20INSERT%20statement%20conflict%20with%20the% 20FOREIGN%20KEY%20constraint%20“ FK_VthaFormsWorkflow_VthaForms”。%20发生%20冲突%20%20in%20database%20“ MyDWPApps”,%20table%20“ dbo.VthaForms”,%20column%20%27Id%27.%20The%20statement %20具有%20been%20已终止。/是
如果我删除最后一句话,则该网址有效。总字符数:359,URL参数:328
http:// localhost123 / Error /发生%20error%20%20,而%20更新%20the%20entries。%20See%20the%20inner%20exception%20for%20details。%20INSERT%20statement%20conflict%20with%20the% 20FOREIGN%20KEY%20constraint%20“ FK_VthaFormsWorkflow_VthaForms”。%20The%20conflict%20出现%20in%20database%20“ MyDWPApps”,%20table%20“ dbo.VthaForms”,%20column%20%27Id%27. / True
//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 logger并将所有内部异常消息传输到nlog logger输出到文件。所有用户错误都会按照@Steve的建议显示一般错误消息。