Display a view from another controller in ASP.NET MVC

后端 未结 7 1185
孤街浪徒
孤街浪徒 2020-11-29 16:06

Is it possible to display a view from another controller?

Say for example I have a CategoriesController and a Category/NotFound.aspx view.

7条回答
  •  萌比男神i
    2020-11-29 16:17

    Yes. By default, ASP.NET MVC checks first in \Views\[Controller_Dir]\, but after that, if it doesn't find the view, it checks in \Views\Shared.

    The shared directory is there specifically to share Views across multiple controllers. Just add your View to the Shared subdirectory and you're good to go.

    If you do return View("~/Views/Wherever/SomeDir/MyView.aspx") You can return any View you'd like.

提交回复
热议问题