Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Automapper和Url.Content_C#_Asp.net Mvc_Automapper - Fatal编程技术网

C# Automapper和Url.Content

C# Automapper和Url.Content,c#,asp.net-mvc,automapper,C#,Asp.net Mvc,Automapper,我有一个MVC5控制器,可以将DTO转换为ViewModel model.Items = items.Select(i => new MovieSearchItem { Id = i.Id, Image = (string.IsNullOrWhiteSpace(i.Image) ? @Url.Content("~/Content/noMovieImage.png") : i.Image), Title = i.Title }).ToList(); 我正试图用Aut

我有一个MVC5控制器,可以将DTO转换为ViewModel

model.Items = items.Select(i => new MovieSearchItem
{
    Id = i.Id,
    Image = (string.IsNullOrWhiteSpace(i.Image) ? @Url.Content("~/Content/noMovieImage.png") : i.Image),
    Title = i.Title
}).ToList();
我正试图用Automapper做同样的事情

Mapper.CreateMap<MovieDTO, MovieSearchItem>()
    .ForMember(m => m.Image,
        opt => opt.MapFrom(src => GetConversionForMovieSearchItem()(src)));
Mapper.CreateMap()
.ForMember(m=>m.Image,
opt=>opt.MapFrom(src=>getConversionFormovieArchitem()(src));
调用的函数是:

public Func<MovieDTO, string> GetConversionForMovieSearchItem()
{
    return new Func<MovieDTO, string>(src =>
    {
        var image = string.IsNullOrWhiteSpace(src.Image) ? @Url.Content("~/Content/noMovieImage.png")
                            : src.Image;
        return image;
    });
}
公共函数GetConversionFormoVisioArchitem() { 返回新函数(src=> { var image=string.IsNullOrWhiteSpace(src.image)?@Url.Content(“~/Content/nomoviemage.png”) :src.Image; 返回图像; }); } 执行映射时,会引发异常:

mscorlib.dll中发生“System.ArgumentException”类型的异常,但未在用户代码中处理

其他信息:值不在预期范围内

堆栈跟踪是:

at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
   at System.Web.Hosting.IIS7WorkerRequest.GetServerVariableInternal(String name)
   at System.Web.Hosting.IIS7WorkerRequest.GetServerVariable(String name)
   at System.Web.WebPages.UrlRewriterHelper.WasThisRequestRewritten(HttpContextBase httpContext)
   at System.Web.WebPages.UrlRewriterHelper.WasRequestRewritten(HttpContextBase httpContext)
   at System.Web.WebPages.UrlUtil.GenerateClientUrlInternal(HttpContextBase httpContext, String contentPath)
   at System.Web.WebPages.UrlUtil.GenerateClientUrlInternal(HttpContextBase httpContext, String contentPath)
   at System.Web.WebPages.UrlUtil.GenerateClientUrl(HttpContextBase httpContext, String contentPath)
   at System.Web.Mvc.UrlHelper.GenerateContentUrl(String contentPath, HttpContextBase httpContext)
   at Pinicules.Presentation.Controllers.MoviesController.<GetConversionForMovieSearchItem>b__0(MovieDTO src) in c:\Repositories\GitHub\Pinicules\Pinicules.Presentation\Controllers\MoviesController.cs:line 23
   at lambda_method(Closure , MovieDTO )
   at AutoMapper.DelegateBasedResolver`2.Resolve(ResolutionResult source)
   at AutoMapper.NullReferenceExceptionSwallowingResolver.Resolve(ResolutionResult source)
   at AutoMapper.PropertyMap.<ResolveValue>b__6(ResolutionResult current, IValueResolver resolver)
   at System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable`1 source, TAccumulate seed, Func`3 func)
   at AutoMapper.PropertyMap.ResolveValue(ResolutionContext context)
   at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap)
在System.Runtime.InteropServices.Marshall.ThroweExceptionForhr内部(Int32 errorCode,IntPtr errorInfo)
at System.Runtime.InteropServices.Marshal.ThroweExceptionForHR(Int32错误代码)
位于System.Web.Hosting.IIS7WorkerRequest.GetServerVariableInternal(字符串名称)
位于System.Web.Hosting.IIS7WorkerRequest.GetServerVariable(字符串名称)
在System.Web.WebPages.UrlRewriterHelper.wastThisRequestRewrited(HttpContextBase httpContext)
位于System.Web.WebPages.UrlRewriterHelper.wasRequestRewrited(HttpContextBase httpContext)
位于System.Web.WebPages.UrlUtil.GenerateClientUrlInternal(HttpContextBase httpContext,String contentPath)
位于System.Web.WebPages.UrlUtil.GenerateClientUrlInternal(HttpContextBase httpContext,String contentPath)
位于System.Web.WebPages.UrlUtil.GenerateClientUrl(HttpContextBase-httpContext,String-contentPath)
位于System.Web.Mvc.UrlHelper.GenerateContentUrl(字符串contentPath,HttpContextBase httpContext)
在c:\Repositories\GitHub\Pinicules\Pinicules.Presentation\Controllers.MoviesController.b_uu0(电影到src)中的Pinicules.Presentation\Controllers\MoviesController.cs:第23行
lambda_法(闭包、电影拍摄)
在AutoMapper.DelegateBasedResolver`2.Resolve处(ResolutionResult源)
在AutoMapper.NullReferenceExceptionsWallowIngressolver.Resolve(ResolutionResult源)处
在AutoMapper.Property映射图b__6(解析结果电流,IValueResolver解析器)
at System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable`1 source,TAccumulate seed,Func`3 Func)
位于AutoMapper.PropertyMap.ResolveValue(ResolutionContext上下文)
在AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMappingStrategy.MapPropertyValue(ResolutionContext上下文、IMappingEngineRunner映射器、Object mappedObject、PropertyMap PropertyMap)
为什么会发生这种错误?为什么我可以使用Automapper


谢谢

您的视图模型是什么样子的?
public class MovieSearchItem{public int Id{get;set;}public string Title{get;set;}public string Image{get;set;}}
在转换中使用Url.Content似乎有问题,但我不明白为什么不能使用它。