Models not loaded by Glass Mapper - asp.net-mvc

Could anyone tell me why the models that I have in an external library (referenced in my main project) can't be loaded by Glass Mapper?
I get something like this:
Type Test.Project.Models.Item has not been
loaded
The version of Glass Mapper is 2.0.11 and Sitecore has the version 7.2 (client's request).
Thank you

When you install Glass.Mapper using the Glass.Mapper.Sc.CastleWindsor it will automatically load classes configured with attributes from the current assembly, but if you want to load classes configured with attributes from another assembly. you have to add adding another SitecoreAttributeConfigurationLoader
public static IConfigurationLoader[] GlassLoaders(){
var attributes = new SitecoreAttributeConfigurationLoader("Project.Website");
var attributes2 = new SitecoreAttributeConfigurationLoader("Project.Models");
return new IConfigurationLoader[]{attributes, attributes2};
}

Related

How to use a shared resource for localization in .net core 2 in controllers?

I'm new to .net core development after years of working in .net framework on mostly webform applications.
I'm trying to localize a new project and looking at options determined for this particular use that a shared resource would be the most maintainable solution long term and followed this example: https://damienbod.com/2017/11/01/shared-localization-in-asp-net-core-mvc/
This appears to work great for adding the localized data in the view but I am struggling to be able to do so in the controller such as returning a localized error when something is caught server side and a custom message would be returned.
In my controller's I added
private readonly LocService _SharedLocalizer;
Within a view's method in the controller if I try and add
ViewBag.localizedmessage = _SharedLocalizer.GetLocalizedHtmlString("message")
I get a null error on accessing the page on this line.
If I create a new instance within the view's method I am not sure what to provide as the argument value for the IStringLocalizerFactory.
_SharedLocalizer = new LocService();
What is the piece I am missing or how do I go about properly accessing the shared resource in a controller?
Try this
public class YourController : Controller
{
private readonly LocService _SharedLocalizer;
public YourController(LocService localizer)
{
_SharedLocalizer = localizer;
}
}

Asp.Net Core Localized Resource in Separate Assembly

I am trying to use the new Localization features of .NET Core, but outside the simple sample Microsoft has provided here, https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization#resource-file-naming.
I have my Controllers in a separate project, ProjectA.Controllers, while I have a shared resource class in a common project, ProjectB.Localization. I've configured my startup class as prescribed in the docs.
I am unclear on what to name my resource file and where exactly to put it. I've configured the option to store in the directory "Resources". Is that in the Web project or my ProjectB.Localization where my SharedResource class is? The docs say that if it's a separate assembly, the full namespace should be used. So I've named it, "WorldCart.Facilities.Localization.SharedResource.es.resx" and placed it in the resources folder of the website.
When I run the web app, and debug in the home controller, I do not get a translated string, I get the english version.
Any ideas?
Very late answer, but it might help someone...
I had a similar situation where I had to have the resource files in separate common assembly instead of having it inside the mail web/api project (Core 2.1). The reason being, I could be using the localized resources from other assemblies like Business or DAL layer for throwing warning/error/information messages. This is what I did:
Assume that my web project namespace is MyApp.Web and my resources are in separate class lib MyApp.Resources. In the resources library, create a folder (optional), say "Messages", and create a class Messages.cs. Create the resource files inside the same folder adhering to the naming conventions. For example, Messages.fr.resx.
In the ConfigureServices method of the main project, add the localization without any resource path*:
services.AddLocalization();
services.Configure<RequestLocalizationOptions>(
opts =>
{
/* your configurations*/
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en"),
new CultureInfo("fr")
};
opts.DefaultRequestCulture = new RequestCulture("en", "en");
// Formatting numbers, dates, etc.
opts.SupportedCultures = supportedCultures;
// UI strings that we have localized.
opts.SupportedUICultures = supportedCultures;
});
And in the Configure method, add app.UseRequestLocalization();
In your controller, inject IStringLocalizer<Messages> localizer, where Messages is the class you created in the Resources library. All your localized resources will be available in the localizer object, i.e., localizer["your key or default text"].
The reason for not adding any ResourcePath in the services.AddLocalization(); options is due to the reason that both the resource files (Messages.fr.resx) and the dummy class (Messages.cs) are in the same path. The framework will check for the resource file relative to the class which we have specified in IStringLocalizer<>. If the Messages.cs was in the root folder of MyApp.Resources lib and the resource files were inside folder "xyz", then the configuration should be services.AddLocalization(ops => ops.ResourcesPath = "xyz");
UPDATE - Responding to the queries in the comments:
MVC Views
In MVC Views, the documented approach uses IViewLocalizer, but does not support resource sharing. So you can inject IStringLocalizer<> in the view for using the shared resources. For example:
#inject IStringLocalizer<Messages> localizer
<h2>Information - #localizer["Shared resource access in MVC Views"]</h2>
Data Annotations
In order to use shared resources in the data annotations, you can use the factory method in the service:
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddDataAnnotationsLocalization(options => {
options.DataAnnotationLocalizerProvider = (type, factory) =>
factory.Create(typeof(Messages));
});
where the Messages in the typeof(Messages) is your shared resource dummy class.

grails view parameters bind with non domain class

I want to bind view page params to non domain class(say *.java class).
Project projectInstance = new Project(params)
where Project is not Domain class, it is simply java class.
Does grails provide any plugin or any way to bind it to non domain class.
Thanks.
You cand try with bindData.
In controler:
Project project = new Project()
bindData(project, params)

Universal App Resource resw values in different views

I try to create a Universal Windows App which has a main window containing different views.
ContentFrame.Navigate(typeof(SimplePage));
where ContentFrame is a XAML Frame and SimplePage is a view.
The project has two localisations. Therefore I created a folder Strings in the solution containing two folders, en and de, containing each a Resources.resw file.
I want to use a string from the resw-file inside the SimplePage-view. Therefore I tried:
tbSimpleInput1.Text = ResourceManager.Current.MainResourceMap.GetValue("Resources/dataToolDiameter", ResourceContext.GetForCurrentView()).ValueAsString;
I also tried using ResourceContext.GetForViewIndependentUse() instead of ResourceContext.GetForCurrentView() but I always get a NullReferenceException when trying to debug.
What is the correct way to access the resources in different views?
Here a screenshot of the solution in Visual Studio:
If you're having a single-project solution, I'd recommend you either to create a Shell - as the Microsoft example recommends, or to use the App.xaml.cs class for localization purposes.
First, in the constructor of either class, get the current ResourceLoader:
// E.g use the static constructor of your App class
static App()
{
_resourceLoader = new ResourceLoader();
}
Now getting a resource (e.g. a text) is very easy:
public static string GetLocalizedString(string key)
{
return _resourceLoader.GetString(key);
}
Now you can load a string form the default resource dictionary:
tbSimpleInput1.Text = App.GetLocalizedString("dataToolDiameter");
Please note: this only works as long as you use the default pattern for localization in your project. If you use different resource files, you'd have use an overload of the ResourceLoader constructor.

Embedded resource mapping files added - class not mapped

In my asp.net mvc application I'm using nhibernate 3.2 as ORM. I have catalogue structure like
..\Models\Persons with User.cs and OtherClass.cs in it and also ..\Mapping with files hibernate.xml,Persons.xml I added mapping files as embeeded resources and in code of application I have
Assembly assembly = Assembly.GetExecutingAssembly();
config.AddAssembly(assembly);
sFactory = config.BuildSessionFactory();
I tried to run simple hql query
From User up WHERE up.Password=admin AND up.Login=admin
but I still get an error:
A first chance exception of type 'NHibernate.Hql.Ast.ANTLR.QuerySyntaxException' occurred in NHibernate.DLL
User is not mapped [ FROM User up WHERE up.Password=admin AND up.Login=admin]
I can't get an idea what I'm doing wrong. I'd appreciate any help.
Can you check if the mappings are loaded by inspecting the sessionfactory properties after creation?

Resources