The /Views/Shared/Error.cshtml view (View) file for the ASP.NET MVC 5 project templates from Visual Studio 2017 all the way to Visual Studio 2022 is wrong. When I was teaching the ASP.NET MVC 5 course, I had to Google the full example of this file or look for a version I had written before. Today’s article explains the problem and provides a complete example program for me to quickly find the correct Error View file in the future.

## Problem Description

When you create an ASP.NET MVC 5 project template in Visual Studio, the /Views/Shared/Error.cshtml file contains the following.

  1 2 3 4 5 6 7 8 9 10 11 12 13 14   錯誤



Note: The <hgroup> is not a legal HTML5 tag, so it should be removed! 🔥

If your Web.config’s <customErrors> is set to mode="On".

 1 2 3   

When any exceptions occur in your Controller, the rendered HTML is output with the contents of the Layout and Error view pages, and the result looks like this.

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63   - 我的 ASP.NET 應用程式

© 2022 - 我的 ASP.NET 應用程式



This means that there are two copies of the final HTML. Every time I see this result, I feel very dumbfounded. Because there is no new version of ASP.NET MVC 5 anymore!

NET Core MVC has been solved.

## Solution

So, the correct /Views/Shared/Error.cshtml file should look like this.

 1 2 3 4  @model HandleErrorInfo



Here @model HandleErrorInfo is the Model that can get the error information when the Error error page is displayed, and it is the Model that should be used to get the exception information in the Error View view page. For example, you can use the following method to get the exception message content.

 1 2 3 4 5 6  @model HandleErrorInfo

@Model.Exception.Message


For a more complete example of error message display, you can refer to the following code snippet.

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74  @model HandleErrorInfo

• ControllerName
@Model.ControllerName
• ActionName
@Model.ActionName
• Exception
@Model.Exception.ToString()
• @{ if (Model.Exception is null) {
• InnerException
@(Model.Exception.InnerException?.ToString())
• } } @{ if (Model.Exception is System.Data.Entity.Validation.DbEntityValidationException ex) {
• DbEntityValidationException
@foreach (var eves in ex.EntityValidationErrors) { foreach (var ves in eves.ValidationErrors) {
1. @(ves.PropertyName + ": " + ves.ErrorMessage))
2. } }
• } }
• User
@User.Identity.Name
• Url
@Request.Url
• SessionID
@Session.SessionID