11

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

Siamak Ferdos
  • 3,181
  • 5
  • 29
  • 56
  • Please share some code for more understanding.. – mmushtaq Jul 20 '16 at 06:49
  • Without looking at your code, the crazy solution would be to use the viewEngine to create the view dynamically and use it across as many projects as you want. – mahlatse Jul 20 '16 at 08:39
  • I append some code to show my mean – Siamak Ferdos Jul 20 '16 at 11:26
  • Check this thread [Is it possible to access MVC Views located in another project?](http://stackoverflow.com/questions/24341336/is-it-possible-to-access-mvc-views-located-in-another-project) – Paresh Jul 23 '16 at 05:44
  • 1
    As you can see, the Razor Engine limits your access to Views. They should be within your Views Folder, and CANNOT be outside your Web Project Folder. It's **very bad etiquette** to down-vote answers just because you don't like what people are telling you. You don't have to upvote correct answers, but If the answers you get in good faith are accurate, and you down-vote, people lose interest in helping you. – Dave Alperovich Jul 23 '16 at 16:40
  • 1
    - follow the [link](http://stackoverflow.com/a/238363/2012249) for the above query – Himanshu Sharma Aug 02 '16 at 09:07

1 Answers1

1
  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

  • 1
    I should separate projects for some reasons and I'm looking to find a way to share views between applications – Siamak Ferdos Jul 23 '16 at 06:19
  • 1
    You asked a question, I answered it, is it wrong? You should consider down voting only when answer is either wrong or irrelevant. And of it is wrong then please provide the correct thing. – Mohammed Dawood Ansari Jul 23 '16 at 15:39
  • Yes. Your answer is irrelevant. My question is 'Path in PartialView method'. So that the types of the set paths in Partial View are my mean. – Siamak Ferdos Jul 23 '16 at 16:04
  • All true, except that 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. In all fairness, this answer should be re-worded. – Dave Alperovich Jul 23 '16 at 16:38
  • 1
    @SiamakFerdos, this answer is correct in reference to your question. Razor Paths will only work for files inside the Web Project Folder. Not liking an answer is a bad reason for down-voting it. – Dave Alperovich Jul 23 '16 at 16:39