I want to do the following:
when url does not have instID, I want to redirect to the "Instelling" action
In this controller , each method needs an 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 this is in the controller.
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";
the thing is this: it really works, but the action itself is first executed, THEN the redirect occurs. This is not what I wanted.
Perhaps I should not use actionfilters, but just add a route? in this case, how would I redirect the route to another controller if instID is missing?
source share