Hi I am trying to redirect a page when certain condition is not page using PartialViewResult method . But i am unable to do it. My code is like as follow :
public PartialViewResult Visit()
{
int visit = 0;
if (base.IsLoggedIn() == true)
{
visit++;
}
else
{
// toDO redirect to home/index page
}
}
The code snippet you provided will not compile. You must return a result from all code paths. Try something like this:
UPDATE:
I believe I now understand what you’re trying to accomplish. In the event an ajax request is made by a user who is not logged in, you would like the browser to redirect the user. I suggest modifying your action method as follows:
Assuming that you have configured in your web.config, the response will be a 302 Redirect which is not what you want in this scenario. See Phil Haack’s blog post which explains how this ASP.NET behavior can be prevented and a 401 Unauthorized response can be returned:
http://haacked.com/archive/2011/10/04/prevent-forms-authentication-login-page-redirect-when-you-donrsquot-want.aspx
Then, assuming you’re using jQuery to issue the AJAX request, you can handle the 401 response: