2

I know how to do redirect from parent controller, suppose I have

public class _ParentController : Controller {  
    ...
}

public class HomeController : _ParentController {  
    ...
}

I can add a method to _ParentController:

protected override void OnActionExecuting(ActionExecutingContext filterContext) {
    if (condition) {
        filterContext.Result = Redirect(path);
    }
}

Now I need to return a view or do Server.Transfer (I need to preserve the url). Server.TransferRequest doesn't work for me in this case, is there any other way to do what I need? I use .NET MVC3 and IIS7.5

Thanks.

3
  • 2
    Did you try filterContext.Result = View(somePameter); ? Commented Jan 24, 2013 at 10:42
  • "I know hot to do redirect from parent controller, suppose I have " is it how to do ? Commented Jan 24, 2013 at 10:46
  • @tschmit007 awesome, thanks, it's what I need, simple but I didn't figure out it myself. If you add it as an answer I'll mark it. Commented Jan 24, 2013 at 10:49

2 Answers 2

2

For example you can have:

protected override void OnException(ExceptionContext filterContext)
{
    base.OnException(filterContext);
    if (((filterContext.Exception is SecurityException)) ||
        ((filterContext.Exception is AuthenticationException)))
    {
        filterContext.ExceptionHandled = true;

        filterContext.Result = View("Error", "You don't have permission");
    }
}

This will set the result of action to view of your choice, preserving current url. (keep in mind that view have to be found in folder according to current route or in shared folder)

filterContext.Result = View("Name of view", "object model")';
Sign up to request clarification or add additional context in comments.

Comments

1

Did you try

filterContext.Result = View(someParameter);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.