I can't find a way to read resource values in a controller
If you're referring to string resources stored in App_GlobalResources then you can add a
using Resources;
and access them via
Strings.<name of resource>
or use
Resources.Strings.<name of resource>
Related
i have a PropertyController, which I use to serve a bunch of pages. For example..
/Property
/Property/{id}
/Property/add
/property/edit/{id}
I now need to do a bunch of stuff based on a particular property I will need to do serve pages like this:
/Property/{id}/images/add
/Property/{id}/images/edit/{id}
/Property/{id}/rooms/add
/Property/{id}/rooms/edit/{id}
I think I need to build a new ImagesController and RoomsController, but do I need to but these in a folder structure? My RouteConfig is currently set to the default MapRoute rule ({controller}/{action}/{id}
You don't need to reflect your routing structure in your folders structure.
Check this one out:
ASP.Net MVC support for Nested Resources?.
Effectively your routing string is a regExpression to match whatever comes in from a requester. And if there's a match it's trying to bind all the variables in your expression to values from the HTTP request.
In regard to creating new controllers - a rule of thumb is to create a controller per resource / business entity. So in your case I would say yes to ImagesController, RoomsController and PropertyController.
URL : http://www.myserver.com/movies/details/19
Can someone tell me how the number '19' in the URL is used by the MVC code for the controller and views
(I already know how it relates to a particular representation of the domain data)
If you look at the default route configuration, you'll see that it is built as "/{controller}/{action}/{id}". If you stick with that and use (int id) as your parameter for the action, that is the value passed into id.
Edit: And just in case you need to find that, you can look in the App_Start folder for the RouteConfig.cs file.
What the various pieces of the URL do depend on how your Routes and Controllers are set up. The asp.net website provides a series of tutorials designed to introduce you to the options:
http://www.asp.net/mvc/tutorials/controllers-and-routing
I have started working on an MVC project and I came across some scenarios where I feel I am stuck. I need to convert the existing MVC3 site to work for multiple language.
I have one HeaderPage.cshtml and it has a view model bound to it by
#model IHeaderPage
And it outputs a property of this model:
<h3>#Model.HeaderName</h3>
I called this view from MainPage.cshtml
#Html.Partial("HeaderPage")
Now in the Controller's Action method I change the model's property
objHeaderPage.HeaderName="Fill your Registeration details";
And when i run the project i see the the text "Fill your Registeration details".
Now how can I change the text value, i.e. it should read from my resx file.
I have already created resx files in App_LocalResources folder.
I heard that, it can be done by Display Attribute.. but how do i do that or is there any other better way?
This should answer your question regarding the use of DisplayAttribute.
I use DisplayAttribute for every property of my ViewModel, but if you have to handle custom messages like "The record can not be saved because of an error...", or something similar, you can simply use
objHeaderPage.HeaderName = Resources.ResourceMessageName;
The framework will choose automatically the correct culture.
I prefer to put all my resources in a separate projects so I can deploy only the dll of the resources in case of need, but you can also think to deploy the resx files to edit them directly on the production machine. I guess it's up to what you prefer/need.
use System.ComponentModel.DataAnnotations Namespace in ViewModel.
[Display(Name="Fill your Registeration details")]
public string HeaderName{get;set;}
also you can use your resource file. Just review http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.aspx
I want to localize my resources (form labels, validation messages etc) using DisplayAttribute. I have my resources in the database, but looks like DisplayAttribute uses the resx files, and the class is sealed itself so I cannot derive a subclass and override the require methods/properties.
Is there any way of handling the resourcemanager used by DisplayAttribute, to get the resources from the database instead of the resx files.
And no, I can not use the resx files.
You will need to create your own ResourceProvider to get the messages from the database, and then set the <globalization> tag in Web.Config to point to your custom ResourceProvider. .Net will then use this instead of resx files.
Detailed explanation: http://msdn.microsoft.com/en-us/library/aa905797.aspx
Using ASP.net MVC v2.0, Any way to change the name of the __RequestVerificationToken cookie? In an effort to conceal our underlying technology stack, I’d like to rename the cookie to something that can’t be traced back to ASP.Net MVC.
More info on this at Steve Sanderson's blog.
ASP.NET MVC 3 and 4 let you change the cookie name by setting the static AntiForgeryConfig.CookieName property.
(Msdn reference here)
I know that the question asks specifically about ASP.NET MVC 2, but this question still returns high up the search engine rankings for appropriate queries such as "ASP.NET MVC AntiForgeryToken cookie name". I thought I'd add the information here to save others from decompiling the ASP.NET MVC 3+ source code like I did.
Looking at the MVC 2 source code I dont think it's possible to change the cookie name. The AntiForgeryData class starts:
private const string AntiForgeryTokenFieldName = "__RequestVerificationToken";
and to get the cookie name it just calls:
string cookieName = AntiForgeryData.GetAntiForgeryTokenName(ViewContext.HttpContext.Request.ApplicationPath);
in the HtmlHelper class. It takes the application path and converts it to base 64 and appends it onto the end of __RequestVerificationToken which is what you see when you view the source.
If you really need to change the name I'd recommend downloading the MVC 2 source code from codeplex and look at creating your own html helper and anti forgery token using the source code as a reference. But in doing this you could always introduce your own bugs...