Friday, July 29, 2016

Path in PartialView method ( to share them between applications)

Leave a Comment

I want to share some Partial View between some applications. I tried different solutions which are suggested in internet, but none of them not work! But I hope by using absolute or relation path in PartialView method it works but I don't know is it possible or no.

So there are 2 questions here:

  1. If I create a common project in my solution as a sub domain, can I use a URL of it in other project's PartialView method?
  2. If I create common folder in my solution, I use a path something like "~/../Common/myView.cshtml". In this case Visual Studio not take error to me on editor by on runtime I got an error ("Cannot use a leading .. to exit above the top directory"). So is any solution to use a path of a common folder outside the root?

I know that it'was better that I separate these question into 2 ones, but as there was no solution to share partial views I gather them here.

As you can see there is a Common folder in my solution which contains _MainMenu.cshtml and also I have this file in Shared Folder in Website project too.

How should I write PartialView in Admin controller to path to each of them?

I want some code like these:

For Common folder:

return PartialView("~/../Common/_MainMenu.cshtml");

For shared folder:

return PartialView("http://localhost:16287/Module/Menu/_MainMenu");

enter image description here

2 Answers

Answers 1

You can try to read view from dll by using VirtualPathProvider.

1) You must register your VirtualPathProvider within the Global.asax Application_Start handler.

2) You must call the view in your DLL using the special path like so:

return View("~/Plugin/YOURDLL.dll/FULLNAME_YOUR_VIEW.aspx"); 

3) OR

I would suggest avoiding cshtml files and generating HTML manually in your HTML helpers. TagBuilder class does a great deal of help here.

Answers 2

  1. Can't be.
  2. Not a good idea.

Because that's not code which can be compiled to DLL and can be used by simply referencing it, it will be better you create Areas in you are application or replicate your views.

Edit : Razor Views CAN be compiled. But that doesn't change the fact that no View can be referenced by the Razor View Path resolver if it's located outside the Web Project. Virtual Paths could work. Thanks to @DaveAlperovich

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment