locked
How to return a view in another controller RRS feed

  • Question

  • User-1890023382 posted

    by default,the razor engine will search the view in current controller's folder and shared folder,but how can i return a view in another specific folder?

    Saturday, January 23, 2016 11:15 PM

Answers

  • User1124521738 posted

    mvc view results:

    return View() - will return a view named the same as the action method from the corresponding path of the controller HomeContoller and Action Index will look in ~/Views/Home/ and ~/Views/Shared/ for Index.cshtml or Index.vbhtml

    return View("AltIndex") - will return a view named the same as the view specified by the string from the corresponding path of the controller HomeContoller will look in ~/Views/Home/ and ~/Views/Shared/ for a view named AltIndex.cshtml or AltIndex.vbhtml

    return View("~/Views/SpecialIndex.cshtml") - will return the view specified by the string and will return ~/Views/SpecialIndex.cshtml

    You can also write alternate view engines that provide alternate paths/search routines, the above is based on the out-of-box behavior

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 24, 2016 1:00 AM

All replies

  • User1124521738 posted

    mvc view results:

    return View() - will return a view named the same as the action method from the corresponding path of the controller HomeContoller and Action Index will look in ~/Views/Home/ and ~/Views/Shared/ for Index.cshtml or Index.vbhtml

    return View("AltIndex") - will return a view named the same as the view specified by the string from the corresponding path of the controller HomeContoller will look in ~/Views/Home/ and ~/Views/Shared/ for a view named AltIndex.cshtml or AltIndex.vbhtml

    return View("~/Views/SpecialIndex.cshtml") - will return the view specified by the string and will return ~/Views/SpecialIndex.cshtml

    You can also write alternate view engines that provide alternate paths/search routines, the above is based on the out-of-box behavior

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 24, 2016 1:00 AM
  • User-14642827 posted

    Try return RedirectToAction("Index") for example. This should get you to where you wish to go.

    Sunday, January 24, 2016 5:46 AM
  • User-1383778792 posted

    Redirect by Route

    Return RedirectToRoute("Stuff");

    Redirect by Action

    RedirectToAction("Index", "Stuff");

    Redirect by Specific Folder and files

    return View("~/Views/Stuff/index.cshtml")

    Sunday, January 24, 2016 8:23 AM
  • User-1890023382 posted

    Thank you so much sir.I'm only one step from success,I didnot add the suffix cshtml.and the viewengine can not find it.it makes me crazy.

    Also thanks to other responders.

    Sunday, January 24, 2016 1:00 PM