Resource files as editable content in a MVC application - c#

A client wants to be able to add any language to the multilingual MVC-based site I'm currently building.
They would be responsible for providing the translations themselves and while the process doesn't need to be enabled through a GUI it needs to be simple enough, so recompiling the project is a no-go. Editing text files (possibly XMLs) is OK.
Using a semicolon-separated list of language codes in the Web.config file (for example: en-US;de-GE and so forth) and using RESX files inside the App_GlobalResources folder I am able to satisfy the conditions... at least as long as the resources are used inside the Razor pages or MVC controllers.
There is a problem, however - any attempt to use the resources in MVC view model classes (such as the Display attribute or the Compare attribute) throws an error:
System.InvalidOperationException: Cannot retrieve property 'Name' because localization failed. Type '[Resource Type Here]' is not public or does not contain a public static string property with the name '[Resource Name Here]'.
There are several questions dealing with this already on StackOverflow, but ALL of them revolve around turning the resource files from content files into embedded resources. While this might make the code function it's no good for me because the resources can no longer be edited by the client!
What else can I do in this case?

In the end I scrapped using the automated mechanisms of MVC, using #Resources.MyResource.DisplayName in Razor files along with properties such as data-val-required added which can also be supplied with resources.

Related

Controlling access to static files in AspNet.Identity

I am working on an MVC project (.NET Framework 4.7.2) that uses AspNet.Identity to control access to pages. I have some static documentation files generated by TypeDoc that I wanted to be able to control access to. Serving the files proved to be no problem, I just created a wwwroot directory and put the files in there. The files can then be viewed via a URL of the form /wwwroot/index.html (etc.).
However, since these are documenting the code, I wanted to be able to lock this down. I have seen articles on how to do this in .Net Core but have yet to track down how to do things using AspNet.Identity. There is, of course, the laborious way of doing this - i.e. recreating every page as a view with a controller and applying a tag like [Authorize(Roles = "Developer")] to the controller but this would defeat the object of being able to generate these files automatically.
The project has out-of-the box files such as App_Start/Startup.Auth.cs and Controllers/AccountController.cs that include AspNet.Identity code, so I am guessing I may need to put some additional code in one of these.
I also note that serving the static files works despite not adding (or finding) any code like app.UseStaticFiles();, as used in .Net Core.
Any advice/pointers on this would be greatly appreciated!

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/

ASP.NET MVC 3: Change resource file texts in web frontend

I'm facing the following situation:
Complex MVC application with multiple projects
Should be able to handle multiple languages (not every language is already defined)
The resource keys will always be available, just the values are
different per language
Service layer and web layer needs resources
Resources are in its own project
Resources are public, with build action embedded resource and custom tool PublicResXFileCodeGenerator
The game changer:
The resource texts for each language should be editable in the web frontend and should be on the fly available
And here is the problem. Reading and displaying the contents of each resource in the frontend is no problem.
Reading the localized resource files on behalf of the set CultureInfo:
var resourceSet = ResourceFile.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, false);
Is it possible to change the resource files anyway? What would be another approach? The database way?
I found this approach. But it probably won't work within our environment since we have a seperate project for the resources and needs them in multiple projects.
My (simple) thoughts about the database way:
Every needed string for every ViewModel would be defined in ViewModelBase >:/
Controller gets the needed strings per View and set locale out of repository and sets them into the ViewModel
View would access the resources per ViewModel.Key
Any thoughts?
Thanks!
Is it possible to change the resource files anyway?
No, not with resource files. You could write a custom resource provider and store them in another storage such as a database for example.
Late answer: We stored all the resources in the database and coded a custom resource provider.

Categories

Resources