ASP.NET MVC, CustomErrors & ResponseRewrite

I have an MVC website (v5, although I don’t think it is connected), where I intentionally entered an error while trying to establish a connection to the database (wrong server IP address in the connection string). When the user clicks on the HomeController, one of the dependencies for the constructor is UserRepository (to get the current user profile data), which depends on the database connection / session. When this is not the case, the Resolver Dependency cannot enter the UserRepository, and when it does, it causes an error (as with any dependency of any controller), and I get the general β€œNo parameterless constructor defined for this object.” This is pretty useless.

So I'm trying to use a custom error page to catch an internal exception and display it in a friendly way. (Since this error occurs when trying to get a HomeController, it never reaches the HandleErrorAttribute attribute, therefore, relies on CustomErrors).

So, I have an ErrorsController with a series of actions ...

Snippet from ErrorsComtroller.cs

public ActionResult Error()
{
    return View("Error_500");
}

public ActionResult NotFound()
{
    return View("Error_404");
}

Snippet from web.config

<customErrors mode="On">
  <error statusCode="404" redirect="~/errors/notfound" />
  <error statusCode="500" redirect="~/errors/error" />
</customErrors>

Error_500 , HandleErrorInfo, , , Server.GetLastError(). , GetLastError() null, , " ". , , CustomErrors. web.config, ...

web.config  

, , GetLastError() . Thing, ASP.NET .

. , . .

, , intellitrace, . , , HomeController, "HomeController". ,

/errors/error

, , . customerrors WITH ResponseRewrite redirectmode, . ( ) ErrorsController.Error(), . , , ~/errors/error, .

MVC- URL- CustomErrors, ResponseRewrite?

+4
1

" , ResponseRewrite Server.Transfer , . , , .aspx .html:"

<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Error.aspx"/>

: https://dusted.codes/demystifying-aspnet-mvc-5-error-pages-and-error-logging

"-, Server.Transfer MVC, , , Server.Transfer /Error/Anywhere, , 404!"

: CustomErrors redirectMode = "ResponseRewrite"

, ResponseRewrite .

, , . MVC HTTP. error.cshtml (.. ) Views\Shared - , - . HTTP-, (, 404, 500 ..). : HTTP- , URL (. -).

HTTP "", , "" (, ), HTTP. , , , (, , ), ( 2 ).

HTTP. , :

1.) ( 200), , ( !)

2.) URL- URL- -

. . ( customErrors web.config): https://dusted.codes/demystifying-aspnet-mvc-5-error-pages-and-error-logging

-, :

http://benfoster.io/blog/aspnet-mvc-custom-error-pages

https://msdn.microsoft.com/en-us/library/bb397417.aspx

https://www.youtube.com/watch?v=nNEjXCSnw6w

Asp.Net Mvc 3?

, ​​, : , ResponseRewrite. CustomErrors (.. CustomErrors mode = "Off" ). , .

, , - , 200 ( OK) .

+1

Source: https://habr.com/ru/post/1537557/


All Articles