Sri Ram

Sri Ram

  • 1.6k
  • 173
  • 99.8k

Using Response.Redirect in MVC action

Jun 21 2017 10:00 AM
I have an action method in a controller class as shown below and based on a if condition, system should redirect to MVC page. But, Response.Redirect is not working as expected and throws an exception "Cannot redirect after HTTP headers have been set".

  1. [HttpGet]  
  2.         public ActionResult GoToPage(string pageName)  
  3.         {  
  4.             var path = GetPagePath(pageName.Trim());  
  5.   
  6.             if (!string.IsNullOrWhiteSpace(path))  
  7.             {  
  8.                // framing the fully qualified path  
  9.                 path = string.Format("{0}/{1}", ConfigurationFactory.GetConfigurationProvider().GetAppBaseUrl(), path);  
  10.   
  11.                 if (!path.ToLower().Contains(".aspx"))  
  12.                 {  
  13.                    Response.Redirect(path));    error line  
  14.                 }  
  15.                 return Redirect(path);  
  16.             }  
  17.             else  
  18.             {  
  19.                 return RedirectToAction("AccessDenied""Error"new { area = "" });  
  20.             }  
  21.         }  
Can someone help on this?

Answers (1)