Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6927737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:05:40+00:00 2026-05-27T11:05:40+00:00

Hi I am trying to redirect a page when certain condition is not page

  • 0

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
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:05:41+00:00Added an answer on May 27, 2026 at 11:05 am

    The code snippet you provided will not compile. You must return a result from all code paths. Try something like this:

    public ActionResult Visit()
    {
        int visit = 0;
    
        if (base.IsLoggedIn() == true)
        {
            visit++;
            return PartialView();
        }
        else
        {
            return RedirectToAction("ACTION_NAME_GOES_HERE");
        }
    }
    

    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:

    public ActionResult Visit()
    {
        int visit = 0;
    
        if (base.IsLoggedIn())
        {
            visit++;
            // whatever else needs to be done
            return PartialView();
        }
        else
        {
            // Note that this could be done using the Authorize ActionFilter, using
            // an overriden Authorize ActionFilter, or by overriding OnAuthorize().
            if (!base.IsLoggedIn())
                return new HttpUnauthorizedResult();
        }
    }
    

    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:

    $.ajax({
        url: '/your_controller/visit',
        type: 'GET',
        statusCode: {
            200: function (data) {
                // data = your partialview
            },
            401: function (data) {
                location.href = '/the_path/to_redirect/to';
            }
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to redirect a page via my .htaccess, but it does not
am trying to redirect the page using a header location, but it seems using
I'm trying to redirect a page via JavaScript: window.location('http://www.google.com'); but in firefox, I'm getting
I'm trying to redirect a page using javascript output using php. The problem I'm
I'm trying to redirect users that login to their profile page but get the
I am trying to redirect a page using HTTP 410 status using php header
I'm trying a redirect on a page that goes like this: original URL: http://www.foo.com/products/
I'm trying to redirect a page after of item added in method itemAdded in
I Have the following code i am trying to redirect the page to the
I am trying to redirect a page reading the url from the config file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.