16

I have a couple of websites I am building that display the same or similar data in pages. To reduce code duplication, what i did was created a 3rd project/website and figured I'd use that as the location for shared HTML, java-script, CSS, images, etc.

I am able to include JavaScript, CSS, and images fine from the shared project using relative paths. however i am not able to render partial pages of HTML. when trying to access the two websites that are trying to load partial page using code like this:

@RenderPage("/SharedArtifacts/Views/MySharedViewscshtml")

I get the following response:

The virtual path '/SharedArtifacts/Views/MySharedViewscshtml' maps to another application, which is not allowed.

Now I've googled and tried using ~ prefix to specify the root of the path to solve the problem, but to no avail... still the same error.

Any thoughts on how to resolve this issue?

4
  • 4
    Compile your asp.net mvc Razor views into a seperate dll Commented Oct 23, 2015 at 3:49
  • 2
    I would look at creating your own NuGet packages and Feed and share the views in your projects that way. The feed can as simple as a Folder location on you File System. Commented Oct 29, 2015 at 16:10
  • hmm. a bit perplexed this is not natively supported. i would thinking sharing common html among different projects/website is a common thing Commented Oct 29, 2015 at 21:31
  • I agree with @StephenMuecke on this one. they should be in a separate dll. You can take a look at this for more info as well hqlsoft.com/community/article/5/… Commented Nov 2, 2015 at 18:14

4 Answers 4

5
+25

We do this all the time. The simple (non-programming) way is to create virtual folders in IIS that map to a shared folder common to all the companion sites. We commonly do this for error result pages and other generic content.

/Views/SharedVirtual/somefile.ascx

The path /Views/SharedVirtual is mapped via IIS's virtual folders. Then in controllers we can do a return View("/Views/SharedVirtual/somefile.ascx",somemodel);. It also works with RenderPartial as well.

Locally when working with the projects I use mklink to create a junction point in the project folder that maps to the shared common folder. That way VS thinks the folder is local when its not and can run the project locally for debugging.

We typically don't use Razor but the concept should work just the same since its not VS that is doing the share mapping, its the OS and IIS that is.

One caveat is that if you edit the views, they will change in all projects so you must be careful with your edits so ensure they will be compatible when used across all the projects that reference them.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Wolfie. Can you confirm for me then, the mklink for local purposes would sit under the usual ~/Views path under that project, where the rest of the views live (in their respective sub-folders)? Any hitches in publishing from Visual Studio given this setup? I'm not immediately sure what roadblocks I could expect -- shared virtual folders would be entirely new for us.
yes. map using mklink to mount the folder in your project's .\Views folder. Right right click on the linked folder in the VS project explorer, and "Exclude". Publish all you want.
3

Views you want rendered reside outside the web app root. This is not allowed at least for security reasons.

What you can do, however, is embed the veiws (.cshtml) as resources by selecting "Embed as Resource" build action instead of the default one. Then, using NuGet package manager install and use a Virtual Path Provider capable of rendering the already embedded views like here, here, or here.

Comments

0

ASP.NET MVC will look in the same assembly for the view. Putting the View or HTML in a different assembly will be awkward for the framework. Internally I don't think the framework supports locating the View/HTML outside the scope of the assembly. You may need to write some custom code or extend the framework, its open source.

Comments

0

When I need to share code between multiple projects I usually use the open as link feature of visual studio. This allows you to have one file for multiple projects.

https://msdn.microsoft.com/en-us/library/windows/apps/jj714082(v=vs.105).aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.