Specifically, I have a phone number that is used in a bunch of views(and a couple actions), and I would like to centralize that somewhere in case it changes. Is web.config good enough? Is there a better place for this? Ideally, I wouldn't have to recompile if the value changed. Thanks!
Update: So far, I like the partial view the best, the main reason being no recompile, no adding it to viewmodels. Another option I've explored is Application_Start in global.asax and using the Application dictionary(although it sounds like the use of this dictionary is frowned upon in mvc). One thing to note is that I need to use this number in some actions also. Any more thoughts/opinions?
In no uncertain terms do you want a view accessing your web.config file.
If this is something which shows up in many places in your views and requires a static set variable, then make a partial view to display the number, and call the partial view multiple times.
The advantage is that you are keeping it in a re-usable area so that if you change it once, it changes everywhere, and also that you are not going to have any separation of concern violations by having your view make calls where it shouldn't have access. Updating the partial will also not require a re-compile.
/Shared/_PhoneNumberPartial.cshtml
<span>555-1234</span>
Used in a view
<div>Phone Number<br/>
#Html.Partial("_PhoneNumberPartial")
</div>
Resx Files, resource: http://msdn.microsoft.com/en-us/library/ekyft91f(v=vs.90).aspx
Config Files, resource: How to use .NET configuration files (app.config, settings.settings) to save and restore all application data?
Class Files, resource: http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff769510(v=vs.105).aspx
Settings Files, resource: http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx
also if you would like to be able to update the settings without touching the database, it will be a great idea to save it in database or xml files. Then create an interface(web page) which communicates with that table and updates the settings. Create a repository which caches the settings every time app starts and invalidates the settings every time one gets updated.
For this case specifically, I would save it in class file. but the best solution depends on the project, later scale of it, and etc, etc.
For something like a phone number I'd put it in a data storage of some kind. If you don't need a full database consider something simple like a file based storage or even a simple *.json or *.xml file, preferably in the AppData folder. However if you do this you should be passing it to your view via a ViewModel and let your controller or some other method do the actual reading from your data storage. Views should not access these directly.
I find the web.config is best suited for configuration settings. It is a config file after all. It's not the best place to put content. Your phone number is content, and to me Content should be in a dynamic and structured location.
You can add the phone number to appSetting key/value in web config like below:
<appSettings>
<add key="PhoneNum" value="1234567890"/>
</appSettings>
Then you can use it
using System.Configuration;
string Phone = ConfigurationManager.AppSettings["PhoneNum"].ToString();
Related
I am working on multilingual site developed in asp.net MVC. Currently we are managing the translation task using resource (resx) file and everything is working fine.
Now as per client requirement, they want to integrate our resource file to a TMS "phrase" through a webhook. So in future, if they create any new key or modifying the existing resource file. Its automatically reflects in application resx file and it should automatically reflects on dev/test/prod environment.
As I tried to update the resource file on API call, its get modified and changes are reflected on application.
But when we modified the resx file under app_GlobalResources folder then it restarts the whole application. so this is one of drawback to use this approach. Also when we deploy our changes then it makes the dll of app_globalreources. Post deployment, unable to add new or make changes in existing translation.
Can any one suggest a best approach, which we should consider to fulfill above requirement.
Edit:-
Can we use json instead of resx file in existing application.
A common way to do translations is through database instead resource files. You save the same information in your database: language, key (the resource name) and value (the translated text).
With this focus, you must develop a way to do translations (the typical CRUD operations) and some layer to get any key in each language.
Talk with your client and check how important is this feature. I worked in a project like this some time ago and, at the end, we never do translations in this way. We add more functionality, made changes, translations and, when iteration finished, we move to production everything. Maybe not your case but it's a pity work on something that later hasn't use.
I have an ASP.net MVC 4 project I am working on and am trying to add an ADO.net EF model to it using Database First.
The creation of the model (.edmx file) seems to run successfully and I am left with the desired .edmx model file in the folder I specified. However, I don't appear to be able to see any of the files nested under this model. (From all my research, I should be able to expand the .edmx file and see *Context.vb, *Designer.vb, *.edmx.diagram and *.tt files underneath, but I cannot.)
It should look something like this in the msdn article about Database First (see Step 4):
Upon noticing this issue, I discovered a handful of warnings that appeared after creating the .edmx model, one of which is (I have obscured part of the full path):
The path 'P:\IT\...\DAL\EF.Utility.VB.ttinclude' must be either local to this computer or part of your trusted zone. If you have downloaded this template, you may need to 'Unblock' it using the properties page for the template file in File Explorer.
The project is stored on a network share on one of our servers so it can be included in our daily backups and Windows Shadow copy also. My understanding is that the above warning has appeared because of this and I need to set my machine to trust this location.
I have tried all possible variations I can think of of
this MSDN article
but to absolutely not success at all, the warning remains.
Either I am looking in the wrong place, or I have missed something.
Does anyone know what I can do to remove this warning and gain access to the objects nested below the .edmx model?
Perhaps this could be of some assistance. I've never really dealt with trust issues working across the domain. I lean towards pulling down and working with a local copy.
I'm a first time poster long time listener and I would really be interested in reading about some of your localization architectures and, eventually, to get feedback on our approach (as follows).
I would like some advice on an approach we're thinking of using with resource files. We are using MVC 3.0 and have a website project and a resource project. In the resource project we have a structure which mimics the same structure as the website e.g. controller -> view -> file.
We reference the resx files in the views by importing the resource namespace on the top of the view/control e.g. <%# Import Namespace="MyAppResources.Resources.Website.Home" %> and then reference the resx value we need by using <%= Index.SomeText %> where index is the name of the resource file.
What we were thinking of doing and would love some advice is instead of using this approach is to divide the resource resx structure into website areas and use a helper e.g. LocalizationHelper.GetValue("Home", "SomeText") where "Home" is the name of the resource file and "SomeText" is a value in that resx file. The reason we would do this is not to have to keep compiling the resource project for every small copy change we make (as we may need a quick fix for our deployed environment) and also it will probably be the most commonly used helper in the website project so this would keep things short and consistent. The Localization helper would also store the values in a cached dictionary so if a value is used more than once it would retrieve it from the cache.
Does anyone know of a better approach or improvements we have not thought of?
I would recommend using a database to store the localized values instead of a RESX file.
Using a database would prevent you from needing to make any code/file deployments to update your application. Furthermore, you could build a GUI interface for modifying the localized values (which is a great feature for the site administrators/editors).
This may just be a yes or no type of question but here goes anyway...
From all (well most) of the examples that I've seen for using mvc, it appears that method for creating the dbml file is just drop the entire schema of the database into it and let it autogenerate all of the linq to sql goodness. It appears that you just need one of these (as you can't have duplicate tables in separate dbml files in the same project) but for some reason it would seem like there's a better way to do this...especially when dealing with a large project that has a fair number of tables.
So is this the proper way to go about creating a dbml file to use in a mvc project, just drop the entire table structure into and go to town? If not, how do you do it?
If the schema was large, I think i would be relying fully on a SQLMetal script to generate my *.dbml and backing classes. This way you can just regenerate your entire data model whenever your database gets updated. Otherwise, if a table, view, etc, gets updated in the database you don't have to delete and then re drag-and-drop that table into your visual *.dbml file.
Actually, I am not expert with SQLMetal, but I think you can even use it it to generate everything you need for Linq-toSql and not even require/generate a *.dbml file.
I'm not sure yet - its a problem I'm still working on but I think that the answer is that should it be desirable to have multiple dbml files - effectively views of your data - then you want to host the dbml files in their own projects so that you can have the similar things in multiple namespaces and not have them conflict.
This being the case the next logical step is to put your dbml files/models into their own projects by default and to learn to work with them when set up that way. This will also aid reuse of a model of database where you have more than one application interacting with that database.
There are certainly issues with separating the thing out and also with having multiple dbml files in a a single project (in terms of ensuring that extensions to the classes are implemented conistently in all instances for example) but I've got a case where its not inappropriate.
Good question, answer probably tends towards being "just one" but not in every case...
Personally I prefer to create the classes/association in the .dbml and then generate the database from that.
Just add the following class to your project
partial class MyDataContext {
partial void OnCreated() {
if (!DatabaseExists())
CreateDatabase();
}
}
I have an application where I allow my users to upload a file of any type. I save this in the file system on the server. The application will only be accessed by two users, so I don't need to worry about uploading any dodgy files.
How do I allow my user to press a button on an MVC form to request the file be sent back via the browser and be presented with the standard save/open dialog?
I want to return any type of file, and the example I've found always specify the type of file being returned. Is there a simple example of this?
See FileResult and derived classes.
An alternative approach is to set HttpContext.Response.ContentType to the proper mimetype and then writing the contents of the file with HttpContext.Response.OutputStream.Write ().
Useful if, for example, the data is not in a local file but stored in a database as a binary blob.