为什么AutoMapperExtension的行为与直接实现不同?(Why would an AutoMapperExtension behave differently from a direct implementation?)
我在
AutoMapperConfig
RegisterMappings()
中定义了以下映射:AutoMapper.Mapper.CreateMap<Member, PhotoViewModel>() .ForMember(dest => dest.Show, opt => opt.MapFrom(src => src.ShowPhoto)) .ForMember(dest => dest.Value, opt => opt.MapFrom(src => AutoMapper.Mapper.Map<PhotoValueViewModel>(src)); AutoMapper.Mapper.CreateMap<Member, PhotoValueViewModel>();
此映射映射
PhotoViewModel
两个属性。 首先,它根据src.ShowPhoto
映射Show
属性,然后将PhotoValueViewModel
映射到Value
属性。映射有效! 不幸的是,我有许多需要类似映射的对象。 所以,我尝试将一些实现抽象为AutoMapperExtension方法。 该方法如下所示:
public static IMappingExpression<TSource, TDestination> ApplyPropertyMapping<TSource, TDestination> (this IMappingExpression<TSource, TDestination> iMappingExpression, Expression<Func<TSource, bool?>> show, Expression<Func<TSource, object>> value) where TDestination : PropertyViewModel { iMappingExpression .ForMember(dest => dest.Show, opt => opt.MapFrom(show)) .ForMember(dest => dest.Value, opt => opt.MapFrom(value)); return iMappingExpression; }
我希望这个扩展方法允许我将原始映射定义为:
AutoMapper.Mapper.CreateMap<Member, PhotoViewModel>() .ApplyPropertyMapping(src => src.ShowPhoto, src => AutoMapper.Mapper.Map<PhotoValueViewModel>(src));
但它不起作用! 现在调用
Mapper.Map<Member, PhotoViewModel>(MemberObject, PhotoViewModelObject)
将PhotoViewModelObject.Value
属性设置为null。造成差异的原因是什么?
其中一些对象的定义是:
public class Member { /**Irrelevant Properties Not Shown**/ public Guid? PhotoID { get; set; } public string PhotoFileName { get; set; } public bool? ShowPhoto { get; set; } } public class PropertyViewModel { public object Value { get; set; } public bool? Show { get; set; } } public class PhotoViewModel : PropertyViewModel { public PhotoValueViewModel Value { get; set; } } public class PhotoValueViewModel { public Guid? PhotoID { get; set; } public string PhotoFileName { get; set; } public string PhotoUrl {get {return Utils.GeneratePhotoUrl(PhotoID); } } }
I have defined the following mapping within
RegisterMappings()
ofAutoMapperConfig
:AutoMapper.Mapper.CreateMap<Member, PhotoViewModel>() .ForMember(dest => dest.Show, opt => opt.MapFrom(src => src.ShowPhoto)) .ForMember(dest => dest.Value, opt => opt.MapFrom(src => AutoMapper.Mapper.Map<PhotoValueViewModel>(src)); AutoMapper.Mapper.CreateMap<Member, PhotoValueViewModel>();
This mapping maps two properties of a
PhotoViewModel
. First it maps theShow
property based off ofsrc.ShowPhoto
, and then it maps aPhotoValueViewModel
into theValue
property.The mapping works! Unfortunately, I have many objects that require a similar mapping. So, I tried abstracting some of the implementation into an AutoMapperExtension method. The method looks like this:
public static IMappingExpression<TSource, TDestination> ApplyPropertyMapping<TSource, TDestination> (this IMappingExpression<TSource, TDestination> iMappingExpression, Expression<Func<TSource, bool?>> show, Expression<Func<TSource, object>> value) where TDestination : PropertyViewModel { iMappingExpression .ForMember(dest => dest.Show, opt => opt.MapFrom(show)) .ForMember(dest => dest.Value, opt => opt.MapFrom(value)); return iMappingExpression; }
I'd expect this extension method to allow me to define the original mapping as:
AutoMapper.Mapper.CreateMap<Member, PhotoViewModel>() .ApplyPropertyMapping(src => src.ShowPhoto, src => AutoMapper.Mapper.Map<PhotoValueViewModel>(src));
But it doesn't work! Now calls to
Mapper.Map<Member, PhotoViewModel>(MemberObject, PhotoViewModelObject)
set thePhotoViewModelObject.Value
property to null.What is causing the difference?
The definitions of some of these objects are:
public class Member { /**Irrelevant Properties Not Shown**/ public Guid? PhotoID { get; set; } public string PhotoFileName { get; set; } public bool? ShowPhoto { get; set; } } public class PropertyViewModel { public object Value { get; set; } public bool? Show { get; set; } } public class PhotoViewModel : PropertyViewModel { public PhotoValueViewModel Value { get; set; } } public class PhotoValueViewModel { public Guid? PhotoID { get; set; } public string PhotoFileName { get; set; } public string PhotoUrl {get {return Utils.GeneratePhotoUrl(PhotoID); } } }
原文:https://stackoverflow.com/questions/32915518
最满意答案
该错误意味着您已经有一个规则在某处被声明了名称为
Imported Rule 1
。 尝试更改错误中指示的文件中的规则名称。如果在〜/ public中有一个web.config文件,而在web根目录中有另一个文件,则这两个文件将合并请求到您的静态资产。 这听起来像是在发生什么?
你可以使用多个web.configs来配置不同的路径,但是你不应该在它们之间重复规则。
您还可以使用根web.config中的
<location>
元素来配置不同的路径(作为使用多个web.config文件的替代方法)。The error implies you already have a rule declared with the name
Imported Rule 1
somewhere. Try changing the name of the rule in the file indicated in the error.If you have a web.config file in ~/public and another in the web root both files are merged for requests to your static assets. This sounds like what's happening?
You can use multiple web.configs to configure different paths like this, but you shouldnt duplicate rules between them.
You can also use
<location>
elements in the root web.config to configure different paths (as an alternative to using multiple web.config files).
相关问答
更多-
.htaccess和web.config目录访问阻塞是否可靠?(Is .htaccess and web.config directory access blocking relyable?)[2021-10-15]
这取决于“足够”对你来说是否足够好。 在大多数情况下, deny from all的deny from all足够好的。 根据服务器设置或脚本,可能会利用其中一个可能导致htaccess文件被覆盖的内容。 所以在apache中,您可以在server / vhost配置中设置规则而不是htaccess文件。 最好的方法可能仍然是将敏感文件移出文档根目录。 It depends on whether "enough" is good enough for you. For the most part, the ... -
试试这个。 如果我理解,你想用它来重定向404找不到的方法吗? RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [R=301,L] 或者这可能没有index.php RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FIL ...
-
您可以尝试添加: RewriteCond %{REQUEST_URI} !^/(index\.php)?$ RewriteCond %{REQUEST_URI} !^/wp-[^/]+/ RewriteCond %{REQUEST_URI} !^/templates/ RewriteRule ^ - [L,F] 就在之前: RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d 这里的问题是,固定链接可能会中断 ...
-
漂亮的网址 该框架附带一个public / .htaccess文件,用于允许没有index.php的URL。 如果您使用Apache来为您的Laravel应用程序提供服务,请确保启用mod_rewrite模块 。 如果Laravel附带的.htaccess文件不能用于Apache安装,请尝试下面的一个: Options +FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FIL ...
-
通过htaccess重定向,其中IP不是&&删除index.php(redirect via htaccess where IP is not && remove index.php)[2022-04-05]
发现这很好用: RewriteEngine on # If your IP address matches any of these - then dont re-write RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1 RewriteRule ^(.*)$ http://www.anothersite.com/ [R=302,L] # do not rewrite links to the assets and theme files RewriteCond ... -
该错误意味着您已经有一个规则在某处被声明了名称为Imported Rule 1 。 尝试更改错误中指示的文件中的规则名称。 如果在〜/ public中有一个web.config文件,而在web根目录中有另一个文件,则这两个文件将合并请求到您的静态资产。 这听起来像是在发生什么? 你可以使用多个web.configs来配置不同的路径,但是你不应该在它们之间重复规则。 您还可以使用根web.config中的
元素来配置不同的路径(作为使用多个web.config文件的替代方法)。 The er ... -
尝试这个: RewriteRule ^index\.php/(.*) $1 [NS,NC,L,R=301] 您没有处理的是所有请求都需要重写,当您进行重写时,它会生成一个子请求 - 也可能会被重写。 所以你最后重写/文章到/index.php/articles,然后在子问题中,你重写/index.php/articles到/ articles并生成一个新的301重定向请求,打开和关闭。 添加NS标志将使此规则不对子请求进行操作,我认为应该解决您的问题,除非您还在/ articles - > /index. ...
-
首先,你应该空白 $config['index_page'] = ''; 在config config.php中然后在项目主目录中的.htaccess中编写以下代码。
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L,QSA] < ... -
第1步:在你的.htaccess中替换所有其他代码..用这个: RewriteEngine On RewriteRule ^(application) - [F,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php/$0 [PT,L] 第2步:将.htaccess从应用程序文件夹移动到index.php所在的位置。 第3步:更改config.php 从: $co ...
-
将所有请求重定向到index.php,但/ files子目录除外(Redirecting all requests to index.php except /files subdirectory)[2022-03-30]
像这样的东西: RewriteEngine on RewriteCond %{REQUEST_URI} /img/.+(gif|png|jpg)$ RewriteRule .* - [L] RewriteCond %{REQUEST_URI} /css/.+(css|png)$ RewriteRule .* - [L] RewriteCond %{REQUEST_URI} /js/.+css$ RewriteRule .* - [L] RewriteCond %{REQUEST_URI} / ...