1- Sometimes my SQL Server is down for maintenance, so when I try to run my ASP.NET MVC application i face the yellow error screen with error network-related or instance-specific error occurred while establishing a connection to SQL Server.
Since I don't know the sequence of events in ASP.NET MVC I want to know where I should check connectivity so can I redirect user to a view informing him/her about this situation (instead of displaying this error page).
I saw this answer in asp.net forum but I couldn't understand how can I handle this inside global.asax.
2- Is it possible to do some changes which for every database connection error I can redirect user to that information page? I mean is it possible to define global exception handler for database connection error?
P.S. My connectionString in web.config is named DefaultConnection.
I think you can prepare a global filter(if you have many controllers and don't want to decorate each with an attribute) and just use it to check what kind of exception has been thrown and e.g. redirect user to some custom view:
public class SqlExceptionFilter: IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
// Do your logic
}
}
And then just register it in Global.asax in Application_Start() method:
GlobalFilters.Filters.Add(new SqlExceptionFilter());
Related
I am trying to resolve an issue that involves displaying helpful errors to users while my application is in startup. For example, when a database connection string is improper, the current behavior is that the database connection checking will throw an InvalidOperationException inside the Startup.cs file, which will prevent the host from being created. This causes a 500.30 ASP.NET Core App Failed To Start error page to be displayed in the browser. What I would like to do instead of this is to capture that exception, and open instead my own errorPage.html (or something similar).
Is it possible to open up a page inside of the Startup.cs file of an application? If so, how can I do that? And if not, what would be the best practice on displaying a custom error page so as to not be shown the default page?
I am attempting to create a custom error page display to run in my app when there is an issue with the database connection string.
When I alter the string to something invalid, I receive the error mentioned in the title above.
Is there any way to override this page and show a more informative one that would tell me that my DB connection string is wrong?
There is a InvalidOperationException that is thrown in the Startup.cs file, but I'm unsure on how to extract this from the startup file and use it, when my app fails to start in the first place.
Is this possible to do?
You can disable the default error page by using the disableStartUpErrorPage="true" setting in web.config for the IIS hosting module. This will just fallback to another custom error page of your choosing served by IIS, rather than allowing you to show a dynamic custom page.
Documentation
I am using ASP.NET Identity in my MVC project.
When I make a request for my login page, Configuration method of Startup class is called. In which, Create method of ApplicationUserManager class is called. In this method an exception occurs but custom error page does not render.
My Web.Config :
It should be pretty obvious from the error message
An exception occurred while processing your request. Additionally, another exception occurred while executing the custom error page for the first exception. The request has been terminated.
So your login screen has an error.
ASP tries to execute your ~/Views/Shared/Error.html and return its result.
Step 2 failed because there is some sort of error with executing ~/Views/Shared/Error.html.
So ASP gives you the message you got - it cannot, afterall, go back and use ~/Views/Shared/Error.html to report the error on ~/Views/Shared/Error.html, can it.
I suggest you have a close look at ~/Views/Shared/Error.html so see whether
a) it actually exists, and
b) if it exists, whether it contains any errors of its own.
either a) will be false or b) will be true.
I am still a relatively inexperience with Visual Studios and C# so please let me know if necessary information has not been provided or I am unclear in my description.
I have an MVC 4 project in Visual Studio 2010 Web Developer Express and I am attempting to set up basic Forms Authentication. I have used the ASP.NET Configuration Manager to set up a couple test users and roles. The Configuration Manager has also created the ASPNETDB.MDF database in my App_data folder which has been included in the project.
I am currently using the generated AccountController and Account views.
Whenever I attempt to access a HTTPGET or HTTPPOST method with which has the [Authorize] attribute, a TargetInvocationException is thrown from the following method in the InitializeSimpleMembershipAttribute class:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// Ensure ASP.NET Simple Membership is initialized only once per app start
LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
}
When viewing the details of the exception, the inner exception states that "The ASP.NET Simple Membership Database could not be initialized."
The inner exception of the above inner exception states that it "Could not find the conceptual model type for" a given model.
The model that was not found belongs to an ADO.Net [.edmx] file which is generated from a database. The model that it cannot find is alphabetically the first table/class in the database, so it likely also applies for every class from that database.
I am not sure if it is important, but I use a database separate from the ASPNETDB.MDF to store information enter on the website.
I do not understand why the LazyInitializer.EnsureInitialized() method may be causing this problem.
Have you checked the connection string used by WebSecurity.InitializeDatabaseConnection in Global.asax.cs Application_Start() - this could potentially cause the issue.
If the named connections string is correct are you sure the dbContext connection string is correct:
public SomeContext(): base("MyConnectionStringName")
{
}
I am logging errors in my controllers method:
protected override void OnException(ExceptionContext filterContext)
But if I make a type in my view page, or enter a route that doesn't exist, it doesn't seem to log that erorr?
Use Elmah logging for that. No code required, just configuration. Elmah logs errors automatically in memory, xml, or a database, and provides a very nice userinterface for reviewing the errors.
See Scott Hanselman's explanation, and the official documentation, Using Elmah with ASP.NET MVC
Thats because its a pipeline and you want to log at a different scope, to log this error add the following to your global.asax:
public override void Init()
{
base.Error+=new EventHandler(MvcApplication_Error);
base.Init();
}
The pipeline is basically this:
incomming request
Hits IIS/ ASP.net
then the routing engine
then the controllers
then the views
so you need to get your error handlers in place before routing takes place to catch them.