Peter Kellner
Peter Kellner

Reputation: 15478

How can code go past response.redirect?

I've got the following code block. I'm confused how the code can go past the

Response.Redirect("~..")

Indeed it does. I thought any lines past that would automatically not execute. Am I missing somethig basic here? I find the debugger actually executing the next lines.

    public ActionResult Index()
    {
        Response.Redirect("~/Default.aspx", true);

        string year =
           Utils.ConvertCodeCampYearToActualYear(
               Utils.GetCurrentCodeCampYear().ToString(CultureInfo.InvariantCulture));
        var viewModel = GetViewModel(year);
        return View(viewModel);
    }

Upvotes: 5

Views: 214

Answers (2)

Nicholas Carey
Nicholas Carey

Reputation: 74227

All Response.Redirect() does (really) is set location= response header to the specified URI and sets the http status to 302 Found. It also writes a little stub HTML in the response with a link to the new URI as well, but that's a mere decoration.

Unless you use the overload that allows you to specify whether or not processing should be continued via a bool flag, processing continues. If that bool flag is true, response processing is terminated by aborting the thread processing the request, throwing a ThreadAbortException as a side effect.

Upvotes: 4

Mathew Thompson
Mathew Thompson

Reputation: 56429

You need to return it. It's a function. In your case you can use Redirect:

return Redirect("~/Default.aspx");

Upvotes: 8

Related Questions