c# - Is it possible to render a view when the server is down -


i have render simple view when server down. there global filter checks server connection. inside filter redirect should performed show view when server is. problem redirect controller never occurs. code inside filter executed several times , many requests error in browser. if redirect image or text goes fine. rendering view no-go.

global filter:

public override void onactionexecuting(actionexecutingcontext filtercontext) {     using (var connection = new sqlconnection configurationmanager.connectionstrings["incbbsconnection"].tostring()))     {         try         { connection.open(); }         catch (sqlexception)         {             // works, text shows:             //var content = new contentresult {content = "server down!", contenttype = "text/plain"};             // filtercontext.result = content;              // works, image appears:             //filtercontext.result = new redirectresult("~/content/images/loginlogo1.png");              // doesn't perform redirect controller               filtercontext.result = new redirecttorouteresult("serverdown",new routevaluedictionary(new controller = "error", action = "serverdownview"}));         }                  { connection.close(); }     } } 

since filter executed multiple times until many requests error, problem in how have registered filter.

a filter applied globally using:

public static void registerglobalfilters(globalfiltercollection filters) {     filters.add(new <yourfilter>); } 

however, means filter applied error controller. error controller detect server down , redirect itself.

you can avoid checking controller in filter:

if (filtercontext.controller errorcontroller) {     return; } 

this way, won't redirect if in errorcontroller.

another thing point out, using actionfilter in way not optimal solution. think expect server online of time. code run on each action. instead of using actionfilter can implement exceptionfilter. filter run when happened. inside exception filter can check reason exception , if detect server down redirect error controller.

update

after doing research on how exclude global filter found blog post: conditional filters in asp.net mvc 3.

phil haack describes how can add custom ifilterprovider won't return filter when on error controller. nice implementation control if filter applies outside of actual filter.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -