diff --git a/core/lib/Drupal/Core/AjaxController.php b/core/lib/Drupal/Core/AjaxController.php
index 31259fb..5dd2c68 100644
--- a/core/lib/Drupal/Core/AjaxController.php
+++ b/core/lib/Drupal/Core/AjaxController.php
@@ -51,6 +51,9 @@ public function content(Request $request, $_content) {
     // controller.
     $request->headers->remove('accept');
 
+    // Setup the right theme used in the ajax request.
+    $this->setBasePageTheme($request);
+
     $response = $this->container->get('http_kernel')->forward($controller, $attributes->all(), $request->query->all());
     // For successful (HTTP status 200) responses.
     if ($response->isOk()) {
@@ -74,6 +77,44 @@ public function content(Request $request, $_content) {
     }
     return $response;
   }
+
+  /**
+   * Setup the used theme depending on the request.
+   *
+   * Many different pages can invoke an Ajax request to system/ajax or another
+   * generic Ajax path. It is almost always desired for an Ajax response to be
+   * rendered using the same theme as the base page, because most themes are
+   * built with the assumption that they control the entire page, so if the CSS
+   * for two themes are both loaded for a given page, they may conflict with
+   * each other. For example, Bartik is Drupal's default theme, and Seven is
+   * Drupal's default administration theme. Depending on whether the "Use the
+   * administration theme when editing or creating content" checkbox is checked,
+   * the node edit form may be displayed in either theme, but the Ajax response
+   * to the Field module's "Add another item" button should be rendered using
+   * the same theme as the rest of the page. Therefore, system_menu() sets the
+   * 'theme callback' for 'system/ajax' to this function, and it is recommended
+   * that modules implementing other generic Ajax paths do the same.
+   *
+   * @param Request $request
+   *   The current page request.
+   */
+  protected function setBasePageTheme(Request $request) {
+    if ($theme = $request->request->get('ajax_page_state[theme]') && $theme_token = $request->request->get('ajax_page_state[theme_token]')) {
+
+      // Prevent a request forgery from giving a person access to a theme they
+      // shouldn't be otherwise allowed to see. However, since everyone is allowed
+      // to see the default theme, token validation isn't required for that, and
+      // bypassing it allows most use-cases to work even when accessed from the
+      // page cache.
+      if ($theme === $this->container->get('config.factory')->get('system.theme')->get('default') || drupal_valid_token($theme_token, $theme)) {
+        if (drupal_theme_access($theme)) {
+          $custom_theme = &drupal_static('menu_get_custom_theme');
+          $custom_theme = $theme;
+        }
+      }
+    }
+  }
+
 }
 
 
