Load views from location outside the mvc website - c#

I'm trying to load an MVC view form a folder outside the location of the app.
My app is in C:\dev\myproject\ and the view file i'm trying to load is located in D:\viewsfolder\something is it possible to do that?
I tried passing an absolute path to the return View() method but that didn't work.

This is not supported by standard ASP.NET MVC. You may take a look at the RazorEngine plugin which allows you to render a Razor view from a string.
You haven't really explained why you need to do that by the way. Maybe there's a better approach to your particular problem.

The best way to do this is to have your Views Precompiled into a dll which can be used across multiple locations with only one code base.
This can be done using the Razor Generator Plugin which you can find here:
http://razorgenerator.codeplex.com/

Related

Cannot access to physical HTML file

I'm migrate my old fashion asp.net MVC application to an angular application.
I've created a subset of .html files that contains templates that will provide the HTML code to build my angular components.
Problem
I want to put this html files inside the folder "Views" that already exists on my application. But when I try to access to .html files I receive the following message:
But if I put the .html files outside this folder I can access directly to them:
Questions
Can you tell me why this is happening?
There is anyway that I can access to html files inside the folder "Views"?
To make a long story short, the Views folder is a special folder that holds templates used by actions that are routed by ASP.NET MVC. Because of this, you cannot use it to hold files that are meant to be used directly.
I would suggest ditching the .NET Framework all together since you are going Angular.
If that is not an option or you would like to retain ASP.NET MVC functionality, then simply use another folder (like you have already done by moving the Templates folder to the root).
Try adding an explicit ignore in your RouteConfig
routes.IgnoreRoute("{file}.html");
That should prevent the default routing from taking effect
To make this work, I suggest you take the html markup in your html file and put it on a .cshtml view, that you will render using a GET action in the home controller for instance (as you would normaly do in the MVC pattern).
The other way to achieve this (which I don't recommend), is to add your html file to a deployable folder (like the js folder for javascript files), and than you can call your file using a direct link, however you will lose any capabilities of the MVC pattern this way.
Hope this helps.
As html files are working in views folder, Can you check the path i.e 'Editor/V2/Templates' after 'Views' folder whether it is correct?

VirtualPathProvider equivalent in ASP.NET 5/MVC6?

I'm looking at migration strategies for an ASP4/MVC4 application into ASP5/MVC6. Our current implementation relies on loading cshtml views as embedded resources from DLL's, and we use a VirtualPathProvider to find these cshtml files.
Unfortunately, HostingEnvironment.RegisterVirtualPathProvider seems to be gone from MVC6. The closest thing I can find is IEnvironment.WebRootFileProvider, which is an IFileProvider. So I coded up a test class to see if it would work, and unfortunately, the IFileProvider I coded up is never queried for CSHTML files. I get requests for all of the .JS, .CSS, etc files, but no .CSHTML's.
What I'm looking for here is the ability to hook into the razor engine and provide a CSHTML file loaded from an embedded resource (or really, any other source for that matter) when a view is requested. What should I be looking at here?
Thanks!
Alright, I figured it out by digging around in the source code. I can load views from wherever I want by overriding the File Provider on the Razor View Engine options class:
services.AddMvc().AddRazorOptions(x => x.FileProvider = new EmbeddedFileProvider(typeof(Startup).Assembly));
Now the only problem I see is that there appears to be no way to embed resources in an ASP.NET 5 DLL via Visual Studio...

Using Same Code in Different Mvc Solutions

I'm using asp.net mvc 3 and .net framework 4.0 with C#
Let's say I have NivoSlider( a slider ) html code. Also it has js, css and image files.
I want to use the NivoSlider cshtml code and js/css/images as "a project" and I want to add it to different MVC solutions. It will become a plugin some-how.
I can't make it a partialview, because I have to move all the css, js and imges files into new solution. I looked at "mvc areas" but it's not rendering my js and css files ( as a matter of fact; using "Areas" is not a perfect way as you know )... I looked up some plugin based architectures, but none-of-them are easy to implement. I have limited time.
So how can I solve this problem ?
EDIT: I can use this http://razorgenerator.codeplex.com/ - But I'm looking for another solutions if you came up with an idea...
You could create a private Nuget and create a small installation for it. That way you can install, update, and uninstall directly from the package.
You can even make a localized package that you copy between computers. Nuget has a very easy way to specify where you want to extract files to, and what files you want to extract.
I'm using razor generator in a commercial project and it's functional but not ideal.
Other possibilities I considered are Add files as link (see Nameless One's answer)
Also overriding the ViewEngine
Can I specify a custom location to "search for views" in ASP.NET MVC?
Or even symbolic links (shortcut links to folders in windows)
https://superuser.com/questions/234422/does-windows7-support-symbolic-links-folder-shortcuts
As recommended above, Razor views can be embedded into assembly as compiled class (by using Razor Generator).
Static resources as .html, .js, .png can be located in the assemble as embedded resource and served by application via VirtualPathProvider (custom or use existing one like https://github.com/mcintyre321/EmbeddedResourceVirtualPathProvider)
Compile your asp.net mvc Razor views into a seperate dll
I wanted to be able to embed compiled Razor views in a dll. This would allow for easy distribution of asp.net mvc ‘modules’ that have their default views embedded, but allowing you to place files in your ‘views’ folder to override those default views.
http://www.chrisvandesteeg.nl/2010/11/22/embedding-pre-compiled-razor-views-in-your-dll/

How to set up locations where view engine should find view in ASP .NET MVC3 or how to render view from another MVC application?

For example I have two similar simple MVC applications.
Application1:
HomeController -> Index action which returns AnotherHome view of
Application2:
AnotherHomeController -> AnotherIndex action which also returns AnotherHome view
Application1 Index action:
public ActionResult Index()
{
return View("AnotherHome");
}
I need to run Application1 and render AnotherHome. When I run app it is obvious that I get the following error:
The view 'AnotherHome' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/AnotherHome.aspx
~/Views/Home/AnotherHome.ascx
~/Views/Shared/AnotherHome.aspx
~/Views/Shared/AnotherHome.ascx
~/Views/Home/AnotherHome.cshtml
~/Views/Home/AnotherHome.vbhtml
~/Views/Shared/AnotherHome.cshtml
~/Views/Shared/AnotherHome.vbhtml
How can I force view engine to search view, for example, in Application2/Views/AnotherHome/AnotherIndex.cshtml ???
The short answer is that you can't render a view that's in a different app's application directory. This is a security issue in IIS, and it won't let you access files outside of its application path.
A longer answer is that you could map a virtual directory in IIS into the app directory somewhere. But this would require specific IIS configuration to manage this, and it's something that can easily break if someone else is maintaining things.
It would probably be better to keep separate copies of the view anyways. What happens if you modify it for one app, and then it breaks the other?
You will have to override the ASP.NET default view engine and override the view paths, however you will have to configure somewhere the location of the second application, unless you want to hardcode it. Refer to this question on how to change the view engine search paths, How to change default view location scheme in ASP.NET MVC?.
To share views between apps, compile the views into a dll (Google for razor generator) and share the dll

Where should I write logic to get menu from database?

I am using Asp.Net MVC 3.0 and I have my _layout.cshtml in Shared folder. Problem is I want to generate menus in this .cshtml which should be loaded from database. But as I understand _layout.cshtml won't have any action etc associated where I can write logic and I don't want to write all this code in cshtml itself. Are there any options to write logic for cshtml within Shared folder?
You can setup a Controller and a view to render the menu and call it inside the _layout.cshtml.
#{ Html.RenderAction("Index", "Menus"); }
Eranga is correct, but let me expand on his answer, to answer your question specifically.
What you can do is create a new controller ("menus" for example), and create an action called default. Have this action return a view, calling it whatever you would like. Now go to your shared folder and add the view using the name you just specified.
Now for the cool part. By default, the MVC framework will look in the controllername/viewname path first, and if it fails it will then look at your shared/viewname path, which is where the view you just created resides! Neat, huh? ;p
Check out http://www.aspnetmvcninja.com/views/view-search-paths for more info on MVC search paths.
#Eranga has given you a good head start on implementing the feature you requested. I think the below two articles will be helpful as well:
Html.RenderAction and Html.Action:
http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx
A sample implementation of Html.Action method with caching:
http://www.tugberkugurlu.com/archive/donut-hole-caching-in-asp-net-mvc-by-using-child-actions-and-outputcacheattribute

Categories

Resources