Mvc 3.0 DataAnnotations Localization - asp.net-mvc-3

Thre's a number of post on StackOverflow as well as other sites around this topic. I was however having hard time finding anything related to MVC3 and it's specific resource keys.
In order to globally localize generic MVC messages one should:
Create a resource file under App_GlobalResources folder
set DefaultModelBinder.ResourceClassKey to the resource name (without .resx)
Create localized copies of MvcResources.resx under that folder and resource name
I've tested this and according to MSDN, if I set the ResourceClassKey to something non existent it should throw exception. It doesn't happen so I must be doing something wrong.
If anyone has got this working under MVC3 and Razor, here's the questions:
where to find the correct MvcResources.resx for MVC3? I was only able to find one for version 2
Where should one set the DefaultModelBinder.ResourceClassKey property? Would that be under Global.asax -> Application_Start or for each request?
Any ideas greatly appreciated.

where to find the correct MvcResources.resx for MVC
http://aspnetwebstack.codeplex.com/SourceControl/latest

Related

MVC3 Dynamic SubDomain Routing

First of all, I have read a lot of post relating this issue like:
Asp.net MVC RouteBase and IoC ,
Tenant-specific routes for dynamically loaded modules ,
and many others.
What I want is:
- Dynamically create pages like tenant1.mydomain.com, tenant2.mydomain.com, etc.
- My tenants will have the same functionality but just different content, styles, title, etc.
I have tried extending RouteBase class but have read that is not a clean solution.
Then I have tried creating a custom RouteConstraint like above posts recommend but not succeded.
Help me!
Thanks!
I have achieved this by doing two things. 1) was to provide functionality to select the correct content by providing the repositories via a factory which was handed the URL on creation. The issue here is that it might be possible to fetch the wrong data via relationships from entities that themselves don't have a tennantid field.
2) Is a basic custom view engine which looks up the host part of the URL and would look for a client specific template (via a folder structure) if the template was found it was used, otherwise the default template is returned.
Between these two I've got a system that delivers (in my case) multiple websites via the same bespoke CMS and product management tool.

Howto dynamically localize the full URL (including Controller, Action and other parameters) in ASP.NET MVC 3

I am looking for a solution to dynamic configure the routing in ASP.NET MVC 3, based on the current language the user is using on the website.
now i have:
domain.com/de/controller/action/subaction/XX
domain.com/en/controller/action/subaction/XX
and i would like to have:
domain.com/de/bereich/aktion/unteraktion/XX
domain.com/en/controller/action/subaction/XX
the name for the controller/action & subaction should be configurable through the Database (the german names as well as the english ones)
As the default route configuration works well in ASP.NET MVC, i would prefer to use it, and e.g. just customize the class that handles the mapping between the URL and the controller (etc).
http://blog.maartenballiauw.be/post/2010/01/26/Translating-routes-%28ASPNET-MVC-and-Webforms%29.aspx this article will help you exactly with your problem but please think about it twice.

MVC3 - Where to place custom attribute classes

I am delving into custom validation attributes and am curious to know how others structure the projects. Where do you typically store custom attributes?
My first thought was to simply create a new folder and be done with it.
Any suggestions?
My first thought was to simply create a new folder and be done with
it.
It would depend on the nature of those attributes and what thety are supposed to do. For example if they are validation attributes you could put them into a Validators folder. If they are action filters you could put them in the ActionFilters folder, etc... so your initial thought is correct. Personally I group those attributes based on their function and place them in a separate folder which indicates this function.
I use 2 different approaches.
Set up a common Class Library to store common validation that will be used on many MVC applications. Then reference this library from your MVC application. You can use http://dataannotationsextensions.org/ to view the source code on how to setup this project.
Place them in folders as suggested by Darin. This folder would be used to store custom validation. If you app was used to keep golf scores a custom validation only to the application could have something to do with a handicap calculation or something specific.
Thanks,

ASP.NET - MVC 3: Localization

I am about to implement localization for my MVC3 web application. Googling my way through large amounts of ways to do it, I was left unsure which way to implement this. I found few ways how to do it:
First option I found was to use App_GlobalResources and ViewData and culture select with Map Routing. (Link)
Second was to make a separate Resources folder in my project and structure it like Model and View folders. Then create the resource files in those folders. To use those resource strings would be like using the Viewbag. Then edit the Global.asax to handle culture changing and create a partial view to allow changing cultures. The instructions to do this are very thorough so this might be the best bet.
(Link)
Third was to use DisplayAttribute and resx-files. This one was a bit hazy, I could not find the sort of information so that I could grasp how this actually is implemented and its restrictions. (Link)
Fourth was to create a custom class to handle resources. This seemed pretty fancy and easy to implement and use but no real information was found if it actually worked. (Link)
Then I found a post that warned about using the App_GlobalResouces and App_LocalResources. (Link)
After going through possible ways of doing localization. I could not find a way which was universally approved or accepted. This left me pondering, which of these, or some I didn't find, would be the best way to implement localization in ASP.NET MVC3?
Then I found a post that warned about
using the App_GlobalResouces and
App_LocalResources. (Link)
I found that post extremely useful. The method explained there is very clean. Here is a snippet of my newly created index view using that method:
#using Resources.Index
#{
ViewBag.Title = "Index";
}
<h1>#Index.Title</h1>
I don't think there is a definitive do-it-this-way-or-else method, so going for the cleanest method seems to me like a good deal.
I've come up with an easier way to handle localization where you don't have to use resource files or attributes. It involves custom metadata providers.
MVC3 uses metadata providers to get all text strings. My approach allows you to either use string tables, a database, flat files or any other source to provide the translations. All you need to do is to inherit two interfaces.
Read about it here: http://blog.gauffin.org/2011/09/easy-model-and-validation-localization-in-asp-net-mvc3/
Edit
Everything is now documented at github and there are nuget packages: https://github.com/jgauffin/griffin.mvccontrib

Render ASP.NET MVC string to View without HttpContext or ControllerContext?

I need to render an ASP.NET MVC view to a string so as to be able to send it via an email (it is an order confirmation email defined in an .ascx file ).
I've successfully been able to render an ASP.NET MVC View to string using one of the methods in this question.
However now I need to be able to do it via a WCF service (that will be accessed via silverlight) and so I don't have a ControllerContext. This WCF service is contained within the same project as my MVC project so has access to all my models etc.
I've looked at several questions on Stackoverflow about this issue, but they all seem to need a controller context. I thought there was something in mvccontrib but it doesn't seem to be there anymore.
The closest I've found is the accepted answer to the aforementioned question, but it unfortunately breaks with RenderPartial within the view you're rendering.
I'm hoping maybe some of the behind the scenes work for ASP.NET MVC 2 related to RenderAction may help make this possible now?
BuildStarted and I have put together a standalone Razor templating engine which you might find useful (Github). You'll need to adapt your .ascx content into a simple string template, but it should do the job.
If you've got NuGet, you can run Install-Package RazorEngine
You may checkout the following blog post. Also Postal is worth looking at.
You need to create a fake HttpContextBase with a fake HttpRequestBase that return meaningful values from their properties.

Resources