0


I have an action like shown below. In GetAvailableBookList, I get the list and if there is not any available book redirect to a message page. But in action part code continues to execute and gets an exception and I find myself in error page. I don't want to use return RedirectToAction or something like that because there are a lot of places where we use this redirect logic in our application.

    public ActionResult ActionName()
    {
        List<BookType> bookList = GetAvailableBookList();
        // some code
        return View("RelatedView");
    }

    private List<BookType> GetAvailableBookList()
    {
        ....
        list  = GetList();
        if(list.Count == 0)
        {
            System.Web.HttpContext.Current.Response.Redirect(messagePageUrl, true);
        }
        else return list;
    }
2
  • My opinion would be that you do not do a redirect at the GetAvailableBookList() level but rather at the ActionResult ActionName() level. Since GetAvailableBookList() gets called from various areas as you have mentioned. Commented May 5, 2011 at 14:26
  • Then I will repeat Redirect in each Action that I do not want to do. It is a specific example. In business part we do this redirect several times for several purposes. For example, while getting a list I may want to check the time if user can see this list now. Commented May 5, 2011 at 14:41

5 Answers 5

1

Unfortunately, Response.Redirect() isn't really friendly with ASP.NET MVC. My rule of thumb is if it comes from HttpContext I don't want to touch it in the controller (of course there are many exceptions to that rule) -- especially since it improves testability.

My suggestion is to use RedirectToAction, but since you don't want to repeat code you can do it in such a way that you don't have to repeat code (although in this case I don't see a problem with repeating code).

public ActionResult LoadBookListAndContinue(
  Func<List<BookType>, ActionResult> continuation)
{
   var list = LoadBooklist();
   if(list.Any())
   {
     return action(continuation); 
   }
   return new RedirectResult(messagePageUrl);
}


// in your controller
public ActionResult ActionName()
{
  return LoadBookListAndContinue(
    list => {
      // some code
      return View("RelatedView");
    });
}

Is it pretty? No, but it works better than the Redirect exception.

Sign up to request clarification or add additional context in comments.

Comments

0

Use

return RedirectToAction("NoListAvailable");

if you have a specific action you would like to execute. The NoListAvailable action can return a view indicating the problem.

Alternatively, you could return the view directly

return View("NoListAvailable");

6 Comments

He said he didn't want to do that.
I don't want to return anything as I stated in the question, I just want to do a classical redirect.
I don't honestly see the difference between RedirectToAction and a classical Redirect. One keeps with the MVC pattern and the other breaks it. Do you really need a Response.Redirect()?
In an action, you have to return something. I want to break the action flow without return something.
An action just has to return a view. When you use a Redirect() you are pointing the browser to another resource, typically a HTML page, which is what a View is. Am I missing something?
|
0

The exception you are getting is probably ThreadAbortException and this is something you cannot avoid unless you allow the thread to continue (2nd argument in Response.Redirect).

On a side note your current solution is generally flawed. You should use RedirectToAction in each action when your method returns an empty list.

1 Comment

The exception is because in the code for example I use something like that: list[0] and since list is null I get exception
0

Throwing a specific exception and redirect where you catch it may be solution

Comments

0

Try to write

System.Web.HttpContext.Current.Response.Redirect(messagePageUrl, false);

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.