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 7069071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:24:04+00:00 2026-05-28T05:24:04+00:00

I am trying to use hijacking to do a single-page web application using mvc.

  • 0

I am trying to use hijacking to do a single-page web application using mvc. The problem that I find is how to toggle the layout that the controller returns in an efficient way.

the most obvious non efficient solution would be to use this code on every controller:

if (Request.IsAjaxRequest())
{
    return PartialView();
}
else
{
    viewdata["controller"]= "x"; viewdata["action"]= "y"; 
    return View("defaultview"); //I need view data because only one template main
                                //view is returned and based on the viewdata the main 
                                //view loads the right partial view inside it
}

Is there a way to do the above code in a more simpler way? Maybe using a custom viewresult, which checks, if the request is AJAX, and returns the proper view. Or maybe some-sort of a base controller? I do not know how to implement both ideas so any guidance would be great.

  • 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-28T05:24:05+00:00Added an answer on May 28, 2026 at 5:24 am

    One possibility is to write a custom action filter and subscribe to the OnActionExecuted event. Inside you will check if the action was invoked using an AJAX request and if it returned a view result. In this case you will substitute this view result with a partial view result:

    public class LayoutSelectorAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var result = filterContext.Result as ViewResult;
            if (filterContext.HttpContext.Request.IsAjaxRequest() && result != null)
            {
                var partialViewResult = new PartialViewResult
                {
                    ViewName = result.ViewName,
                    ViewData = result.ViewData,
                    TempData = result.TempData
                };
                filterContext.Result = partialViewResult;
            }
        }
    }
    

    Now you could decorate your controllers or actions with this attribute.

    If you are using the Razor view engine another possibility is to tweak your _ViewStart.cshtml file which is where the layout is defined:

    @{
        Layout = !ViewContext.HttpContext.Request.IsAjaxRequest() 
            ? "~/Views/Shared/_Layout.cshtml" 
            : null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying use MySql and Entity Framework, using Connector/Net 6.1 with this as
I'm trying to develop an application server that will deliver content to a core
I am trying use Thread but i have some problem (I am beginner at
I'm creating a multiple-tenant application that won't use any of the standard Django Admin
I am trying use the jQuery table sorter plugin for a table that is
I am trying to use Jcrop for my application, though I have run into
I'm currently trying to use a script that ensures users are logged into a
I am trying use the mysql connector in c++ in ubuntu. It appears that
I am trying to address session fixation/hijacking/sidejacking on an ATG/JBoss/Tomcat site. It seems that
I'm hacking around in a Firefox extension, trying to use E4X. However, I've come

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.