简体   繁体   中英

How can I reference a Master view in a different project in ASP.NET MVC?

I'm creating an ASP.NET MVC solution, and inside I had 2 MVC Website projects.

I'm interested in creating a 3rd project called "Shared", where I can reference the shared views/content between the two sites so I have only one place to edit them.

  • WebUI.Site1 (ASP.NET MVC Site)
  • WebUI.Site2 (ASP.NET MVC Site)
  • WebUI.Shared (Newly created ASP.NET MVC Site)

How can I reference a Masterview's location if it is in a different project?

So, normally at the top of a view I would reference a Masterview like so:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Tabbed.Master" Inherits="System.Web.Mvc.ViewPage<WebUI.ViewModels.Admin.EditProfileViewModel>" %>

However,

Now, that "Tabbed.Master" isn't in the local project, it is in a difference project in the solution. I've created a reference to the project but I don't know how to reference Tabbed.Master from WebUI.Site1 to WebUI.Shared?

Something like:

<%@ Page Title="" Language="C#" MasterPageFile="WebUI.Shared.Views.Tabbed.Master" Inherits="System.Web.Mvc.ViewPage<WebUI.ViewModels.Admin.EditProfileViewModel>" %>

Doesn't work.

How can I reference the location of things like masterviews, css files, images across projects like that?

Due to the nature of MasterViews, you need to find a filebased solution. The virtual path is one way to do it, but I think you will get in trouble at some point: How do you handle the file references when you use the internal Visual Studio web server?

And think of the pain you suffer when this virtual path is missing in IIS.

Visual Studio let you link to files from other projects. Those files will be copied over at compile time:

Right-click in Solution Explorer -> Add existing item... -> select files -> Add As Link

Personally, I don't care for this feature. But it seems to do what you need.

Assume that the masterpage lives at WebUI.Shared Root/Views/SharedPage.Master

Try creating a virtual directory under both WebUI.Site1 and WebUI.Site2 pointing to WebUI.Shared :

WebUI.Site1/Shared => WebUI.Shared Root
WebUI.Site2/Shared => WebUI.Shared Root

From within WebUI.Site1 and WebUI.Site2 use the following path to access the masterpage: ~/Shared/Views/SharedPage.Master

I don't think the IDE will be able to keep track of the reference, but it should work on the server.

EDIT: WebUI.Site1 and WebUI.Site2 may need a reference on WebUI.Shared.

You need to create a custom ViewEngine if you want to use a different place than the project's View folder to store your views.

Take a look at this prototype by Phil Haack . It's about storing the views in the database, but it should give you an idea regarding where to start.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM