How to inherits a BaseRazorPage in Razor page - razor-pages

In razor engine way, I can define a BaseRazorPage for all razor views
public abstract class BaseRazorPage<TModel> : RazorPage<TModel>
{
protected BaseRazorPage()
{
}
protected virtual string L(string name)
{
return XXX.Localization.L.Text[name];
}
......
}
Use it in _viewImports.cshtml
#inherits BaseRazorPage<TModel>
Then I can use the L function to do mutiple language in views:
#L("Hello word!")
How can I implement same function in Razor page way? Or is there an alternative way to do this?
The razor page can't inherits any class.

A simple solution would be to create an extension for the PageModel class.
public static class PageModelExtensions
{
public static string L(this PageModel pageModel, string name)
{
// return a new value. put your logic here
return name + "_result";
}
}
Now we can use the L method as a member function.
public class IndexModel : PageModel
{
public void OnGet()
{
string value = this.L("test");
}
}
Or we can use the L method in the view like this
#page
#model IndexModel
#{
ViewData["Title"] = "Home page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>#Model.L("test")</p>
</div>
I hope this helps.
UPDATE
If you want to have a base class with your common methods the following example is what you want.
public class MyPageModel : PageModel
{
public string L(string name)
{
return "sample";
}
}
And your razor page class will look like this.
public class IndexModel : MyPageModel
{
public void OnGet()
{
string value = this.L("test");
}
}
In case you want to inject an object in your base class then you base class should look like this.
public class MyPageModel : PageModel
{
private readonly ApplicationDbContext context;
public MyPageModel(ApplicationDbContext context)
{
this.context = context;
}
public string L(string name)
{
return "sample";
}
}
And the Razor page will look like this
public class IndexModel : MyPageModel
{
private readonly ApplicationDbContext context;
public IndexModel(ApplicationDbContext context)
: base(context)
{
this.context = context;
}
public void OnGet()
{
string value = this.L("test");
}
}

Related

Model mapper mapping Map<String,Object> to class which extends another generic class not working for list field

I am trying to create my custom configuration object from Map using model mapper. Everything gets mapped properly excepts the fields property which is coming fro Generic super class.
My target object is
public class ADParserConfig extends CustomParserConfig<ADParserConfigField> {
private String pattern;
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
}
This extends generic class CustomParserConfig
public class CustomParserConfig<T extends CustomParserConfigField> {
protected List<T> fields;
protected String timeStampField;
public List<T> getFields() {
return fields;
}
public void setFields(List<T> fields) {
this.fields = fields;
}
public String getTimeStampField() {
return timeStampField;
}
public void setTimeStampField(String timeStampField) {
this.timeStampField = timeStampField;
}
}
Where CustomParserConfigField is
public class CustomParserConfigField {
protected String name;
protected Integer index;
protected String type;
protected String format;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getIndex() {
return index;
}
public void setIndex(Integer index) {
this.index = index;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
}
I am trying to map Map using below function
ADParserConfig adParserConfig = getConfig(map,ADParserConfig.class);
public <T extends CustomParserConfig> T getConfig(Map<String,Object> configObject, Class<T> classType){
ModelMapper modelMapper = new ModelMapper();
return modelMapper.map(configObject,classType);
}
Everything excepts fields gets mapped properly for the below map.
{fields=[{name=timeStamp, type=timestamp, format=dd/mm/yyyy HH:MM:SS a}, {name=logName, type=string}], pattern=(?<timeStamp>\d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}:\d{2}\s[AMPMampm]{2})?\s(LogName=(?<logName>[\w\s\W]+))?\sSourceName=(?<sourceName>[\w\s\W]+)\sEventCode=(?<eventCode>[0-9]*), timeStampField=timestamp}
Please help. Why is issue happens only for fields object ? Do I need to specify something else in mapper configurations ?
It looks like a bug and it had been fixed by #370

RazorPages: Model does not get instantiated in Partial with Page Model

Im testing out RazorPages and .Net Core 2.1
I have just taken a new project template and have created a Partial.
These are relevant/added contents of the files.
My problem is
1) Immediate problem: In the partial: OnGetAsync (nor public void OnGet()) does not get called. and I get a NullReference-exceptiion in View on Model on line
#foreach (var item in Model.ImageBE) {
I have tried to cut out DB-call and excplicitly call OnGet from contructor but no difference.
2) I cant to find an example where the Page(index) has an instance of the Partials model (ImageGalleryModel below). but this is the only thing the compiler will accept. Am I doing this totally wrong?
Index.cshtml (the page)
...
[partial name="_ImageGallery" model="Model.ImageGallery" /]
...
Index.cshtml.cs
public class IndexModel : PageModel
{
ApplicationDbContext mContext;
public ImageGalleryModel ImageGallery;
public IndexModel(ApplicationDbContext context)
{
mContext = context;
ImageGallery = new ImageGalleryModel(mContext);
}
public void OnGet()
{
}
}
_ImageGallery.cshtml (the partial)
[table class="table"]
#foreach (var item in Model.ImageBE) {
...
_ImageGallery.cshtml.cs
public class ImageGalleryModel : PageModel
{
private readonly ApplicationDbContext _context;
public IList<ImageBE> ImageBE { get; set; }
public ImageGalleryModel(Photiqo.Data.ApplicationDbContext context)
{
_context = context;
}
public async Task OnGetAsync()
{
ImageBE = await _context.ImageBE.ToListAsync();
}
}
Partials should not have a PageModel file associated with them. If you have C# code that you want to execute, you should consider creating a ViewComponent.
Alternatively, you can move the public IList<ImageBE> ImageBE property to the IndexModel and instantiate it in the OnGetAsync method there. Then you can specify the model type on the partial and pass it to the partial using the tag helper as you currently re doing:
_ImageGallery.cshtml (the partial)
#model IList<ImageBE>
<table class="table">
#foreach (var item in Model) {
...

Call a method in a CORE Razor Page from a ViewModel

My Razor Page
public class IndexModel : BaseModel {
public void OnGet() {
BaseModelMethod();
}
public void LocalMethod() {}
}
calls a method in the base ViewModel
public class BaseModel : PageModel {
public void BaseModelMethod() {
// Do stuff
}
}
Is there a way to call back to the instance of LocalMethod in the calling Razor Page?
You have to define the function as a virtual function. Your BaseModel has to have the following form:
public class BaseModel : PageModel
{
public void BaseModelMethod()
{
LocalMethod();
}
public virtual void LocalMethod()
{
}
}
As you can see I creted the virtual function so that we will know what we kind of method we will call.
Now we can define our own version of LocalMethod like this:
public class IndexModel : BaseModel
{
public void OnGet()
{
BaseModelMethod();
}
public override void LocalMethod()
{
base.LocalMethod();
}
}

AutoMapper property name conversions

I'm trying to register a mapping convention to handle mapping from classes with Pascal Case names to classes with underscore names with postfix and prefix, and back again. I've tried to follow examples, but cannot get my head around how it's supposed to work.
This is one of the many things I've tried, that looks like it should work (in my opinion :)), but it doesn't seem to do anything:
public class PascalCaseEntity
{
public string CallingSystem { get; set; }
}
public class UnderscoreWithPrefixAndPostfixEntity
{
public string p_calling_system_ { get; set; }
}
public class PartsMappings
{
public void Apply()
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile<FromUnderscoreMapping>();
cfg.AddProfile<ToUnderscoreMapping>();
cfg.CreateMap<PascalCaseEntity, UnderscoreWithPrefixAndPostfixEntity>()
.WithProfile("ToUnderscoreMapping");
cfg.CreateMap<UnderscoreWithPrefixAndPostfixEntity, PascalCaseEntity>()
.WithProfile("FromUnderscoreMapping");
});
}
}
public class FromUnderscoreMapping : Profile
{
protected override void Configure()
{
RecognizePrefixes("p_");
RecognizePostfixes("_");
SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
DestinationMemberNamingConvention = new PascalCaseNamingConvention();
}
public override string ProfileName
{
get { return "FromUnderscoreMapping"; }
}
}
public class ToUnderscoreMapping : Profile
{
protected override void Configure()
{
RecognizeDestinationPrefixes("p_");
RecognizeDestinationPostfixes("_");
SourceMemberNamingConvention = new PascalCaseNamingConvention();
DestinationMemberNamingConvention = new LowerUnderscoreNamingConvention();
}
public override string ProfileName
{
get { return "ToUnderscoreMapping"; }
}
}
What am I missing here?
I finally found a working solution. I created two profiles, one for each "direction", and added the mappings to them.
I'm not too happy with it, since I'd rather have the mappings in the same file (grouping them on business area). But at least it works... :)
I also tried putting the registrations in the same Profile, and using the .WithProfile("ToUnderscoreWithPrefix") method, but I didn't get that to work.
Mapper.Initialize(cfg =>
{
cfg.AddProfile(new ToUnderscoreWithPrefixMappings());
cfg.AddProfile(new FromUnderscoreWithPrefixMappings());
});
public class ToUnderscoreWithPrefixMappings : Profile
{
protected override void Configure()
{
RecognizeDestinationPrefixes("P", "p");
SourceMemberNamingConvention = new PascalCaseNamingConvention();
DestinationMemberNamingConvention = new LowerUnderscoreNamingConvention();
CreateMap<PascalCaseEntity, UnderscoreWithPrefixAndPostfixEntity>();
}
public override string ProfileName { get; } = "ToUnderscoreWithPrefix";
}
public class FromUnderscoreWithPrefixMappings : Profile
{
protected override void Configure()
{
RecognizePrefixes("P_", "p_");
RecognizePostfixes("_");
SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
DestinationMemberNamingConvention = new PascalCaseNamingConvention();
CreateMap<UnderscoreWithPrefixAndPostfixEntity, PascalCaseEntity>();
}
public override string ProfileName { get; } = "FromUnderscoreWithPrefix";
}

Property injection with Unity

i encoutered problem with unity, i want to use property injection, here is what i had in my code :
config of the container :
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<GTModelContainer, GTModelContainer>(new HttpContextLifetimeManager<GTModelContainer>())
.RegisterType<IUnitOfWork, UnitOfWorkGT>()
.RegisterType<ILogger, Logger>(new ContainerControlledLifetimeManager())
.RegisterType<ISocieteServices, SocieteServices>() ;
}
SocieteService Class :
public class SocieteServices : ISocieteServices
{
private IUnitOfWork UnitOfWork;
public SocieteServices(IUnitOfWork unitOfWork)
{
UnitOfWork = unitOfWork;
}
}
i tried to use property injection (i can't use constructor injection with custom data annotation) and here what i had done :
public class CodeSocieteUniqueAttribute : ValidationAttribute
{
[Dependency]
public ISocieteServices SocieteService {get; set;}
[InjectionMethod]
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
string codeSociete = value as string;
var societe = SocieteService.getSocieteByCode(codeSociete);
if (societe == null) return ValidationResult.Success;
else return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
}
}
the problem is that the societeService in CodeSocieteUniqueAttribute class is not injected.
Assuming that your class for registering types is publicly accessible and has a IUnityContainer object, ie:
public static class Resolver
{
public static IUnityContainer Container { get; set; }
public static void RegisterTypes(IUnityContainer container)
{
// type registrations here
container.RegisterType<GTModelContainer, GTModelContainer>(new HttpContextLifetimeManager<GTModelContainer>())
.RegisterType<IUnitOfWork, UnitOfWorkGT>()
.RegisterType<ILogger, Logger>(new ContainerControlledLifetimeManager())
.RegisterType<ISocieteServices, SocieteServices>() ;
// Now, set the container
Container = container;
}
}
You could access the container you've built up and resolve these types during method execution.
For instance,
public class CodeSocieteUniqueAttribute : ValidationAttribute
{
[Dependency]
public ISocieteServices SocieteService { get; set; }
[InjectionMethod]
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var societeServices = Resolver.Container.Resolve<ISocieteServices>();
SocieteService = societeServices; // Or, you know, just use this since it's resolved.
string codeSociete = value as string;
var societe = SocieteService.getSocieteByCode(codeSociete);
if (societe == null) return ValidationResult.Success;
else return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
}
}
This is actually pretty standard practice, and this MSDN article describes resolving items during runtime.
Another option is to pop the resolution into a default constructor like this:
public class CodeSocieteUniqueAttribute : ValidationAttribute
{
[Dependency]
public ISocieteServices SocieteService {get; set;}
public CodeSocieteUniqueAttribute()
{
var societeServices = Resolver.Container.Resolve<ISocieteServices>();
SocieteService = societeServices;
}
// the rest of the class omitted for brevity
}

Resources