Using route parameter in razor pages over a certain length causing invalid url error

158 views Asked by At

I am trying to pass an error message to my error page in razor using a routing parameter named {error}. But I keep getting an invalid url error. I've chalked it up to being the length of the url parameter I'm passing as my routing parameter which is causing this issue.

But I don't understand this, since the valid length of a url is 2048 characters. And the length of a string is significantly higher. Any insight would be greatly appreciated.

This returns an invalid url error. Total characters: 404, url parameter: 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

enter image description here

if i remove the last sentence, the url is valid. Total characters: 359, url parameter: 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./True

enter image description here

//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()
        {

        }
    }
}
1

There are 1 answers

0
made_it_ma On BEST ANSWER

Will end up using nlog logger and transferring all internal exception messages to the nlog logger output to file. All user errors will display a generic error message as per @Steve's suggestion.