0

For example I have two similar simple MVC applications.

Application1: HomeController -> Index action which returns AnotherHome view of

Application2: AnotherHomeController -> AnotherIndex action which also returns AnotherHome view

Application1 Index action:

public ActionResult Index()
{
    return View("AnotherHome");
}

I need to run Application1 and render AnotherHome. When I run app it is obvious that I get the following error:

The view 'AnotherHome' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/AnotherHome.aspx
~/Views/Home/AnotherHome.ascx
~/Views/Shared/AnotherHome.aspx
~/Views/Shared/AnotherHome.ascx
~/Views/Home/AnotherHome.cshtml
~/Views/Home/AnotherHome.vbhtml
~/Views/Shared/AnotherHome.cshtml
~/Views/Shared/AnotherHome.vbhtml

How can I force view engine to search view, for example, in Application2/Views/AnotherHome/AnotherIndex.cshtml ???

tereško
  • 58,060
  • 25
  • 98
  • 150
Dmytro
  • 16,668
  • 27
  • 80
  • 130

3 Answers3

1

The short answer is that you can't render a view that's in a different app's application directory. This is a security issue in IIS, and it won't let you access files outside of its application path.

A longer answer is that you could map a virtual directory in IIS into the app directory somewhere. But this would require specific IIS configuration to manage this, and it's something that can easily break if someone else is maintaining things.

It would probably be better to keep separate copies of the view anyways. What happens if you modify it for one app, and then it breaks the other?

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
0

You will have to override the ASP.NET default view engine and override the view paths, however you will have to configure somewhere the location of the second application, unless you want to hardcode it. Refer to this question on how to change the view engine search paths, How to change default view location scheme in ASP.NET MVC?.

Community
  • 1
  • 1
ryudice
  • 36,476
  • 32
  • 115
  • 163
  • 1
    That's not really going to help. IIS won't let you render a page from outside of the apps application directory. This prevents so-called directory traversal vulnerabilities. – Erik Funkenbusch Oct 22 '12 at 06:46
0

To share views between apps, compile the views into a dll (Google for razor generator) and share the dll

Joep
  • 11