Hey there, in order to ensure flexibility and update ascx/aspx files on my server without a need to redeploy my webapp and without the risk of files to be overwritten, I want to store them externally, i.e. not in the asp.net MVC webapp. Nevertheless, I want
them to be part of the app and protected via asp.net authentication, i.e. they should be loaded from a controller method with the [Authorize] tag. Is there any means to achieve this? Thanks very much in advance, Christian
ASP.NET vistualizes the file system with something called a
virtual path provider. You could implment this API and then ASP.NET will call into you to load the file (instead of looking on disk). Your implementation could then load it from a database or anywhere.
Virtual path provider is one way, also you can do this without any tweaks as your views will be within your app(as mentioned in your questiion).... Since by default your views are not compiled. So, you can still deploy ascx/aspx files onto the server without
redeploying your webapp...
Apart from this if you just need a way to organize your folders as per convenience, you can just write a ViewEngine and specify the folder locations from which to load the view....
I blog at http://rajeshpillai.net and have a community startup http://ownabook.org/
(Don't forget to click "Mark as Answer" on the post(s) that helped you.)
Thanks for the great replies. Probably I have to clarify what I need: I want to get a part of my views from a directory outside the wwwroot, but still on the same server, not from a database, neither from a zip-file. Defining a customized ViewEngine seems
to just offer a change of paths within the web app directory. Creating a VirtualPathProvider is not that easy, although I think it's actually the best solution. I wonder if there's a tutorial available which does NOT deal with loading views from a database,
but just from another directory in the file system. I would appreciate any hints on that..
I think you can still use the VirtualPathProvider, to load views from another directory in the file system (cause for VPP, it doesn't matter from where your views are loaded).
Here's a sample of loading a view from a "DLL". You can use the ssame concept for loading a view from any of the filessytem on your webserver as long as they are accessible...
The important thing is when you return view from your controller you stick to this pattern.. and you need to give the full name with extension.. You can wrap this in some helper which buildsup your view path...
return View("/yourviewapp/yourpathtofiew/FULLNAME_YOUR_VIEW.aspx"); // This view should be mapped as a virtual directory or app in IIS.
I blog at http://rajeshpillai.net and have a community startup http://ownabook.org/
(Don't forget to click "Mark as Answer" on the post(s) that helped you.)
chris_abyi
0 Points
4 Posts
Loading external views in ASP.NET MVC
Mar 28, 2012 07:47 AM|LINK
BrockAllen
All-Star
27544 Points
4907 Posts
MVP
Re: Loading external views in ASP.NET MVC
Mar 28, 2012 01:02 PM|LINK
ASP.NET vistualizes the file system with something called a virtual path provider. You could implment this API and then ASP.NET will call into you to load the file (instead of looking on disk). Your implementation could then load it from a database or anywhere.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
thinkrajesh
Participant
1356 Points
232 Posts
Re: Loading external views in ASP.NET MVC
Mar 28, 2012 05:28 PM|LINK
Virtual path provider is one way, also you can do this without any tweaks as your views will be within your app(as mentioned in your questiion).... Since by default your views are not compiled. So, you can still deploy ascx/aspx files onto the server without redeploying your webapp...
Apart from this if you just need a way to organize your folders as per convenience, you can just write a ViewEngine and specify the folder locations from which to load the view....
(Don't forget to click "Mark as Answer" on the post(s) that helped you.)
chris_abyi
0 Points
4 Posts
Re: Loading external views in ASP.NET MVC
Mar 29, 2012 11:08 AM|LINK
Thanks for the great replies. Probably I have to clarify what I need: I want to get a part of my views from a directory outside the wwwroot, but still on the same server, not from a database, neither from a zip-file. Defining a customized ViewEngine seems to just offer a change of paths within the web app directory. Creating a VirtualPathProvider is not that easy, although I think it's actually the best solution. I wonder if there's a tutorial available which does NOT deal with loading views from a database, but just from another directory in the file system. I would appreciate any hints on that..
Thanks a lot again for your help,
Christian
BrockAllen
All-Star
27544 Points
4907 Posts
MVP
Re: Loading external views in ASP.NET MVC
Mar 29, 2012 12:51 PM|LINK
Oh if your views are just in some other directory then you can create a virtual directory in IIS -- easier than a VPP.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
thinkrajesh
Participant
1356 Points
232 Posts
Re: Loading external views in ASP.NET MVC
Mar 29, 2012 12:57 PM|LINK
I think you can still use the VirtualPathProvider, to load views from another directory in the file system (cause for VPP, it doesn't matter from where your views are loaded).
Here's a sample of loading a view from a "DLL". You can use the ssame concept for loading a view from any of the filessytem on your webserver as long as they are accessible...
http://stackoverflow.com/questions/236972/using-virtualpathprovider-to-load-asp-net-mvc-views-from-dlls
The important thing is when you return view from your controller you stick to this pattern.. and you need to give the full name with extension.. You can wrap this in some helper which buildsup your view path...
return View("/yourviewapp/yourpathtofiew/FULLNAME_YOUR_VIEW.aspx"); // This view should be mapped as a virtual directory or app in IIS.
(Don't forget to click "Mark as Answer" on the post(s) that helped you.)