asp.net mvc - MVC using Action Filter to check for parameters in URL. stop action from executing -


i want make following:

when url doesn't have instid, want redirect "instelling" action

in controller, every method needs instid.

        [requiredparameter(parametername="instid", controllertosend="instelling")]         public actionresult index(int? instid) {             //if (!instid.hasvalue) {             //    return redirecttoaction("index", "instelling");             //}              var facts = _db.instellingens.first(q => q.inst_id == instid).facturatiegegevens;              return view(facts);         } 

so in controller.

the actionfilter:

namespace mvc2_nastest.controllers {     public class requiredparameterattribute : actionfilterattribute {          public string parametername { get; set; }         public string actiontosend { get; set; }         public string controllertosend { get; set; }          public override void onactionexecuting(actionexecutingcontext filtercontext) {              if (parametername != string.empty) {                 if (filtercontext.actionparameters.containskey(parametername) && filtercontext.actionparameters[parametername] != null) {                     string s = "test";                     //all                 } else {                     //de parameter ontbreekt. kijk of de controller en de action geset zijn.                     if (actiontosend == string.empty)                         actiontosend = "index";                     if (controllertosend == string.empty) {                         controllertosend = filtercontext.controller.tostring();                         controllertosend = controllertosend.substring(controllertosend.lastindexof(".") + 1);                         controllertosend = controllertosend.substring(0, controllertosend.lastindexof("controller"));                     }                      urlhelper helper = new urlhelper(filtercontext.requestcontext);                     string url = helper.action(actiontosend, controllertosend);                      httpcontext.current.response.redirect(url);                     //filtercontext.httpcontext.response.redirect(url, true);                 }             }              base.onactionexecuting(filtercontext);         }          public override void onactionexecuted(actionexecutedcontext filtercontext) {               base.onactionexecuted(filtercontext);         }      } } 

the thing is: work, however, action first gets executed, redirect happens. not wanted.

perhaps shouldnt use actionfilters add route? in case, how redirect route controller if instid missing?

rather creating action filter (which runs before return of action method), consider changing authorization filter allow redirect alternative controller & action

something (pseudo code):

public class requiredparameterattribute : authorizeattribute {     protected override bool authorizecore(httpcontextbase httpcontext)     {         // read instid querystring         // if instid null, return false, otherwise true     }      protected override void handleunauthorizedrequest(authorizationcontext filtercontext)     {         filtercontext.result = new redirecttorouteresult( new { controller = "mycontroller" , action = "myaction" }  )     } 

}


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -