Can I access a value from resource file inside App_LocalResources folder to the Razor view?
Resources files inside App_GlobalResources folder can be accessed as follows.
<span>#Resources.MyResource.TestMessage</span>
Is there any similar way to access file inside App_LocalResources. If not what is the best way to get a value to the Razor View?
Related
I write #inject IHtmlLocalizer Localizer in _Layout.cshtml, but it isResourceNotFound = true? why?
The structure of the Resources folder might not follow the structure of the Views folder. By convention, _Layout.cshtml is located in the Views/Shared folder. Therefore, your Resources folder should reflect that: resource files for the layout should be in Resources/Views/Shared folder.
I'm using App_LocalResources on my project without problems, that was until i created a aspx page inside a folder, I created the resx file with the same name as i did for others but when the page is inside a folder I cant call the resources for that page.
The way I call resources is:
bt_name.Text = GetLocalResourceObject("bt_name").ToString();
And the soluction struture for the aspx and resx are:
Solution -> Folder -> Page.aspx
Solution -> App_LocalResources -> Page.aspx.resx
What am I doing wrong?
I have three resource files in App_GlobalResources for a view:
ConditionsLabels.en.resx
ConditionsLabels.ua.resx
ConditionsLabels.ru.resx
But I can't get any value.
I specified custom namespaces (ViewRes), but in the view autocomplete suggests the only variant #ViewRes.ConditionsLabels_ua. (it's class)
How can I use that?
Don't use App_GlobalResources for MVC projects. Instead, place your .resx files in a standard folder (say, "Resources" or whatever). Next, select the .resx files in Visual Studio and change the following properties:
Custom Tool: PublicResXFileCodeGenerator
Custom Tool Namespace: Resources
Now you should be able to access your resources normally using Resources.MyResource.String1, etc.
About what I read, the EditorTemplates folder must be located under the Shared folder in the Views folder. Is it possible to change this location?
Thank you.
I've always placed my EditorTemplates beneath each view folder they were related too. For example:
If they were "shared" across multiple views I would place them within the "Shared/EditorTemplates" folder.
Is it possible to change this location?
No, that's hardcoded in the MVC source code. Sorry.
How can I get the path of a cs file in aspx?
Here's the issue: I've created a property class in a property.cs file which is used in an EPiServer admin module. Server.MapPath() will hence return the path of the executing file, which is in a totally different place than my code. I need to get the path of the property.cs file (from inside the property.cs file) in order to dynamically set some relative paths to css and js files. How can this be done?
I hence want to be able to include .js and .css files in this cs file, all files located in the same directory, but the cs file is accessed from the EPiServer UI.
I would highly recommend not doing what you are trying to do. You'll be constructing a brittle dependency on files that should not even by deployed with your project.
If you have web classes that rely on resources like javascript and css, you should use the ClientScriptManager (or ScriptManager for ajax apps) to register the script files onto the page, and the scripts and css should be deployed into their own regular web directory.
If deployment location is a problem, and you're creating some kind of reusable, redistributable module, then I would recommend that you embed the .js and .css files as WebResources in your assembly, and use the script manager to register the scripts to the page that way, with ClientScriptManager.RegisterScriptResource().
I think you should go with including your CSS, JS and other static files as embedded resources.
That will include the files inside the DLL, which makes deployment easier. You can then set up a HTTP handler which will serve you the contents of the embedded files - or use the aforementioned RegisterScriptResource() method.
By embedded the files you don't have to know any file paths.
ASP.NET is compiled, you should never need to read settings from source files at runtime, you should read from configuration files (*.config), if they need to be dynamic, these too can be injected during the page lifecycle via a variety of methods.