Unable to access resources in .net web project - asp.net

How do I access resources in a .Net Web Project. I created a web project and then in App_LocalResources folder added a resx file "AstroWap.resx". Now how do I access values from that. The property My.Resources itself is not available.

Good reference reading to use resource files here

To create an access properties - right click your solution, select properties then click the link to create a resource file. You should see a excel type grid. Type your key value pairs in there.
To access it should be
<Namespace>.Properties.Resources.<ResourceName>
The issue is probably how you have added the resource. It might have circumvented the auto generated code which gives you the above syntax
You can check if it has added the auto gen code by looking in your web project under properties -> resources.resx -> resource.cs.designer. The designer file is the autogenerated file and you can see the access code for the underlying resx file. This assumes you have used the default resx file as above. See image for example in solution explorer
In addition this SO question does give more options for accessing resx files if you have some project constraints. I appreciate not everyone can do the above due to team work practices etc.. It's just what I do. And it does work.

Use following code for it:
<%=GetLocalResourceObject("labelname")%>
in aspx page.
If your page name is Test.aspx then your resource file must have name as Test.aspx.resx, otherwise you can not access the resource file value.

Related

How do I get the ASP.NET content relative path

Doing a project between multiple people, and a few components (web app, services app and some others). We will be storing some information inside the Content folder of the web app so it can be accessed directly from the web server with an href, however other components outside of the web app need to access this folder as well, and since we are sharing the project between multiple people using an absolute path is not an option. What options do we have?
EDIT: Trying to explain it a little better.
What i have exactly is, a web project, a "data project" which is just a dll, a "logic" project which is another dll and a services project which is an exe/service.
Both the web project and service project consumes the methods from the logic, and the logic from the data project. Being the last one the responsable for storing data (in a database) and also in the file system.
This "filesystem" path should be configurable, and we are aiming to put it into the content folder of the web project so multimedia files can be accessed directly rather than doing a byte stream.
Now in the web.config(config file of the web app), and app.config(config file of the services app) i could set the absolute path to web/content (the same for both config files) and the data dll would use it without problems. Now the main problem is that we cannot put an absolute path in the config file because each person works on a different computer with obviously different file paths. So if i could just write something like: ~/project/Web/Content rather than C:/myfolder/stuff/blabla/project/web/content in the config files, with ~ resolving the path to the project, this is what i want! Or maybe better ideas about how to share a folder with these apps without adding absolute paths hardcoded somewhere.
What you want to use is:
Server.MapPath("/Content/filepath.ext");
This will give you the absolute path of a file based on it's position within the website, in this case, from the /Content directory.
For a program external to the website, you have a couple options;
The easiest to implement might be a simple configuration value in the external program which points to the directory. My guess is you've already decided that's not ideal, but it may be the quickest way.
Alternatively, there's a Microsoft .NET assembly which gives you easy access to IIS information (I can't recall its name off the top of my head!). You could use this assembly to find the appropriate website, and retrieve its root directory. I'll see if I can find it and get an example, or maybe someone else will see this and post an answer with that information.
Please check the following method "ResolveClientUrl"
MSDN
Use the ResolveClientUrl method to return a URL string suitable for use by the client to access resources on the Web server, such as image files, links to additional pages, and so on.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveclienturl.aspx

How to deploy XML file with my web application?

I've written an ASP.net web application. In the interest of following the advice in "The Pragmatic Programmer" to put application logic in configuration, I wrote a large XML file that describes various business rules.
When I test the application on my local development workstation, I copy the file to c:\xxxxx\myfile.xml and then write the code to read the file from this location.
What is the correct way to deploy this xml file as part of my web application so that it gets read from the directory in which the web application is deployed?
Update: The XML file in question is for server-side configuration and should never be available for download to the end-user.
If you're not embedding this as a resource, you should:
Create a directory under which this file will reside.
Create the file and set its Build Action to Content.
Create a web.config file in that directory which forbids access to the directory so you don't expose your business rules to the Internet.
Add a setting in your application's main web.config that gives the path to this file relative to the root of the application, i.e. "~/MySecureFolder/MyBusinessRules.Xml".
Have some code that calls HttpServerUtility.MapPath Method to convert the value from the setting in web.config to a virtual path.
I don't know if this is what you want - Click on the XML file, then open the Property Window and find the "Build Action" property. Set the value to "Embedded Resources"
I think what you need to do is:
Add the XML file to your web application project in Visual Studio
Right click on the file and select 'Properties...'
Set the Build Action to 'Content' and Copy to Output Directory to 'Do not copy'
this will ensure that your XML file is deployed along with the rest of your web app.
If you want to make available your XML file from http requests to your server, you should
place it in your web publication folder.
This ASP instruction should help you to find your publication path:
Request.ServerVariables("APPL_PHYSICAL_PATH")

Could not find any resources for the specified culture or the neutral culture

I have to try to use internatinalization with this example.
http://afana.me/post/aspnet-mvc-internationalization-part-2.aspx
But I obtain this problem
Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Resources.Resources.resources" was correctly embedded or linked into assembly "Resources" at compile time, or that all the satellite assemblies required are loadable and fully signed.
I am looking for the solucion in google, but I dont understand the solucion.
I create my resources like this..
Right click on the Solution and then choose the "Add->New Project" context menu command. Choose "Class Library" project type and name it "Resources".
Now right click on "Resources" project and then choose "Add->New Item" context menu command. Choose "Resource File" and name it "Resources.resx"
I read in other example that people create the resources in this file App_GlobalResources, this is relation with my error???
I realise this is an old question, however in case it helps anyone else searching here >>>
You need to set the .resx build action to "Embedded resource" for it to work with Data Annotations as in that example. When you add a .resx to an App_LocalResources folder the build action will be set to Content by default.
Also, in your example above, you added it to another project, so ensure that the Access Modifier is also set to "Public" (double click the .resx file to open the file and the Access Modifier is located along the top of the editor window).
Also ensure that you add a reference to the resources project from your main application.

ASP.NET MVC - Non-Embedded Resource FIle

I currently have a local resource file in an ASP.NET MVC project. The goal of this file is to let a non-programmer edit the text on the webpage. The problem is that the resource file is an embedded resource and compiled on ASP.NET MVC Deployment. This means they would have visual studio to change site copy. This is non-optimal :)
Is there a way to make use of resource files in ASP.NET MVC that would result in a .resx file on the server for an admin-type person to be able to edit?
The other option is to put this in a database and have some front end to edit it, but I would really like to avoid this option as its overly complex for just a few text fields on a small site.
Thanks!
Once way of doing so, is to make sure the resources aren't compiled.
When you add a resource file (e.g. TextResource.resx) you can access the resources in a type safe manner.
For example:
ViewBag.Message = Resources.TextResource.MyMessage;
After you add a resource file (*.resx), select it in the solution explorer and view its properties. Clear the "Custom Tool" property. By default it contains the value "GlobalResourceProxyGenerator". Just delete this value.
This has a downside, however as you can no longer access the resources in a type-safe manner. You'll have to load them using the GetGlobalResourceObject and GetLocalResourceObject methods of the current HttpContext.
For example:
ViewBag.Message = this.HttpContext.GetGlobalResourceObject("TextResource",
"Hello", CultureInfo.CurrentUICulture);
This way you'll be able to manually alter the resource files (*.resx). You could then opt to build an application which can read and modify the contents of the resource files, which are just XML files.
For example:
http://www.codeproject.com/KB/cs/Editing_a_ResourceFile.aspx

File browser control to replace <input type="file">

I'm working on an ASP.NET web application for our corporate intranet users. I have a form where a user should provide a path to the file on the local network (something like "\localServer\someFolder\someFile.ext") without uploading the actual file. The issue is that users don't want to type the whole file path and want to use some kind of visual browse dialog.
The standard HTML <input type=file> element allows to browse for a file, but most of the browsers (except for IE) don't allow to access file's full path, so I think it should be done by some external component like Silverlight, Flash, Java applet etc.
I tried to do it with Silverlight, but I'm getting a SecurityException when trying to access file's full path using Silverlight's OpenFileDialog class.
This java applet http://jumploader.com/demo_images.html seems to do something similar to what I'm looking for, but it's focused on uploading files - I only need to be able to get file's full path and pass it to the server as a string.
Any suggestions would be appreciated.
Telerik ASP.NET AJAX RadFileExplorer has the functionality you're looking for:
http://www.telerik.com/products/aspnet-ajax/fileexplorer.aspx
You can use their Custom File Content Provider to hook the GUI to your server's file system.
http://demos.telerik.com/aspnet-ajax/fileexplorer/examples/server-sideapi/dbfilebrowsercontentprovider/defaultcs.aspx
This should be possible with Flash's uploading capabilities. SWFUpload has an API that you may be able to access from JavaScript to extract the selected file name without actually uploading anything. See docs here, for example getFile():
getFile is used to retrieve a File Object from the queue. The file retrieved by passing in a file id (the id property from a file object) or a file index (the index property from a file object).

Resources