Reputation: 17373
Could anyone please help me why the following code not working? The redirect doesn't occur while debugging:
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
base.Initialize(requestContext);
Redirect("/Error/UnAuthorized");
}
Upvotes: 0
Views: 1369
Reputation: 3380
In MVC you should return a redirect result, rather than using Response.Redirect.
Also, I'd say with the error you're getting (headers sent), you're processing this too late in the request.
Here's an example of redirecting from an action method.
[HttpGet]
public ActionResult Basecamp()
{
if (!GetPlanPolicyForUser().IntegrationEnabled)
{
Log<ApplicationsController>.Action( "..." );
return Redirect("/applications/notsupported");
}
//...
}
Upvotes: 1
Reputation: 1038720
The following should work:
protected override void Initialize(RequestContext requestContext)
{
base.Initialize(requestContext);
var url = Url.Action("UnAuthorized", "Error");
requestContext.HttpContext.Response.Redirect(url);
}
This being said, you should not be doing any redirects in the Initialize
method. Personally I've never had to override this method. And even worse, it seems that you are handling authorization in this method which is bad. I would strongly recommend you using a custom Authorize attribute for this purpose.
Upvotes: 4