resource files are useful in web apps


Jeff Key
January 16, 2002

Visual Studio.NET makes it very easy to use resource files, so why not take advantage of them? You can store just about anything in them, including strings, images and persisted objects, but the most useful for web apps is strings. I was originally using them for error message management, but realized that they could offer more, especially in a text-intensive application where having packaged units might be beneficial. Another advantage is their read-only nature; people (ie. clients) can’t modify the contents unless you provide them with an application that writes satellite assemblies.

Availability
Resource files (.resx, .resource) can be compiled into an assembly or put into their own assembly as a satellite resource. When compiled into an app, you’re stuck with what you have at compile time, but that’s not a problem if you’re simply wrapping up something that won’t change between builds. International satellite assemblies require special consideration, but the support for them in the runtime is very cool and invaluable if the need arises.

Using in web apps
The .NET framework gives us a number of tools that help us manage string resources, especially caching and the new Session and Application classes. These work wonderfully when the backing provider is a database. The strings can either be cached at load time if the application knows the string IDs/keys and can iterate through them, or on every request (check the cache, if it’s not there, load value from the database, then cache). This works well in most, if not all, instances. A feature lacking, though, is easy rollback. Compiling resources into satellite assemblies affords a physical history, as well as incredibly flexible portability.

Creating a resource file
In my specific case, I found that my error messages were long enough to make my code look messy and decreased the readability. Simply putting the error messages into a local resource file (compiled with the assembly) made the code much easier to follow.

You can add a resource file via the "Add Item…" item in the Solution Explorer. When you open the file, you’re presented with VS.NET’s XML editor, as the resx format is a simple XML document. The columns presented are Name, Value, Comment, Type and MimeType. When using strings, you only need to be concerned with the Name and Value columns.

Reading from the resource
As with everything .NET, there are a number of different ways to get at the resources. The ResourceManager class offers the simplest way. The most useful constructor takes an assembly and the root name to pick the resources from. For example, if you have a resource in your application ("MyApplication") with the name of "res.resx", you would create an instance with the following:

ResourceManager rm = new ResourceManager("MyApplication.res", Assembly.GetExecutingAssembly())

Once you have a reference to the ResourceManager, it’s just a matter of calling its GetString method.

string s = rm.GetString("TheKey");

This simple feature, in conjunction with the String class’s static Format method, makes for powerful string handling. Instead of:

throw new Exception("The file ‘" + filename + "’ cannot be deleted from the ‘" + dir + "’ directory because of the following error: " + ex.Message + ". Please ensure that that file is not currently being used.")

I can put the following string (name = "FileDeleteError) into the resource file:

The file ‘{0}’ cannot be deleted from the ‘{1}’ directory because of the following error: {2}. Please ensure that that file is not currently being used.

And reference it with the following:

throw new Exception(String.Format(rm.GetString("FileDeleteError", filename, dir, ex.Message)));

Much nicer.

Dynamically loading and using satellite resource assemblies
Creating and using satellite resources is surprisingly easy. The simplest route is to create an assembly containing only resx files.
  

  1. Create an assembly containing (but not limited to) resx files and compile. (I called it "SatelliteResource.dll" in this example.)
  2. Create a reference to the assembly in the project that will use it, or place in its bin folder. The latter is preferred if you will be dynamically placing the files during runtime.
  3. In the calling program, create an Assembly object and load it with the assembly containing the resource.

    Assembly ass = Assembly.Load("SatelliteResource");
  4. Create a resource manager, passing in the new assembly and the file you want to read.

    ResourceManager rm = new ResourceManager("SatelliteResource.res", ass);
  5. Read from the resource.

    string s = rm.GetString("TheKey");

Summary
While using resources in every app (especially web apps) may not be necessary, it is surely something that is useful when the need arises. The advanced capabilities not covered here, including serialized objects and images, can be used more widely in traditional Windows Forms applications and services.

转载于:https://www.cnblogs.com/coollzh/archive/2004/03/01/1917.html

Group az webapp : Manage web apps. Subgroups: auth : Manage webapp authentication and authorization. To use v2 auth commands, run "az extension add --name authV2" to add the authV2 CLI extension. config : Configure a web app. connection : Commands to manage webapp connections. cors : Manage Cross-Origin Resource Sharing (CORS). deleted [Preview] : Manage deleted web apps. deployment : Manage web app deployments. hybrid-connection : Methods that list, add and remove hybrid-connections from webapps. identity : Manage web app's managed identity. log : Manage web app logs. sitecontainers : Manage linux web apps sitecontainers. traffic-routing : Manage traffic routing for web apps. vnet-integration : Methods that list, add, and remove virtual network integrations from a webapp. webjob : Allows management operations for webjobs on a web app. Commands: browse : Open a web app in a browser. This is not supported in Azure Cloud Shell. create : Create a web app. create-remote-connection : Creates a remote connection using a tcp tunnel to your web app. delete : Delete a web app. deploy : Deploys a provided artifact to Azure Web Apps. list : List web apps. list-instances : List all scaled out instances of a web app or web app slot. list-runtimes : List available built-in stacks which can be used for web apps. restart : Restart a web app. show : Get the details of a web app. ssh [Preview] : SSH command establishes a ssh session to the web container and developer would get a shell terminal remotely. start : Start a web app. stop : Stop a web app. up : Create a webapp and deploy code from a local workspace to the app. The command is required to run from the folder where the code is present. Current support includes Node, Python, .NET Core and ASP.NET. Node, Python apps are created as Linux apps. .Net Core, ASP.NET, and static HTML apps are created as Windows apps. Append the html flag to deploy as a static HTML app. Each time the command is successfully run, default argument values for resource group, sku, location, plan, and name are saved for the current directory. These defaults are then used for any arguments not provided on subsequent runs of the command in the same directory. Use 'az configure' to manage defaults. Run this command with the --debug parameter to see the API calls and parameters values being used. update : Update an existing web app. To search AI knowledge base for examples, use: az find "az webapp" You have 2 update(s) available. They will be updated with the next build of Cloud Shell.
05-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值