本文介绍了如何创建一个文件来填充HttpContext.Current.Request.Files?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在我的Web API,岗位操作方法上传服务器上的文件。In my Web API, The POST action method uploads a file on server.有关单元测试这种方法,我需要创建一个HttpContext的,把一个文件其请求里面:For unit testing this method, I need to create a HttpContext and put a file inside its request:HttpContext.Current.Request.Files到目前为止,我伪造的HttpContext这个code这完美的作品:So far I am faking HttpContext with this Code which works perfectly: HttpRequest request = new HttpRequest("", "http://localhost/", ""); HttpResponse response = new HttpResponse(new StringWriter()); HttpContext.Current = new HttpContext(request, response);请注意,我不希望使用起订量或其他任何惩戒库。Note that I DON'T want to use Moq or any other Mocking libraries.我怎样才能做到这一点? (MultipartContent也许?)How can I accomplish this? (MultipartContent maybe?)感谢推荐答案我最终能够通过大量使用的伪造文件添加到的HttpContext 为的WebAPI单元测试反射,因为大部分的 Request.Files 基础设施的谎言隐藏起来在密封或内部类。I was eventually able to add fake files to HttpContext for WebApi unit tests by making heavy use of reflection, given that most of the Request.Files infrastructure lies hidden away in sealed or internal classes.一旦添加下面的code,文件可以相对容易地添加到 HttpContext.Current :Once you've added the code below, files can be added relatively easily to HttpContext.Current:var request = new HttpRequest(null, "http://tempuri.org", null);AddFileToRequest(request, "File", "img/jpg", new byte[] {1,2,3,4,5});HttpContext.Current = new HttpContext( request, new HttpResponse(new StringWriter());通过所做的繁重:static void AddFileToRequest( HttpRequest request, string fileName, string contentType, byte[] bytes){ var fileSize = bytes.Length; // Because these are internal classes, we can't even reference their types here var uploadedContent = ReflectionHelpers.Construct(typeof (HttpPostedFile).Assembly, "System.Web.HttpRawUploadedContent", fileSize, fileSize); uploadedContent.InvokeMethod("AddBytes", bytes, 0, fileSize); uploadedContent.InvokeMethod("DoneAddingBytes"); var inputStream = ReflectionHelpers.Construct(typeof (HttpPostedFile).Assembly, "System.Web.HttpInputStream", uploadedContent, 0, fileSize); var postedFile = ReflectionHelpers.Construct<HttpPostedFile>(fileName, contentType, inputStream); // Accessing request.Files creates an empty collection request.Files.InvokeMethod("AddFile", fileName, postedFile);}public static object Construct(Assembly assembly, string typeFqn, params object[] args){ var theType = assembly.GetType(typeFqn); return theType .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, args.Select(a => a.GetType()).ToArray(), null) .Invoke(args);}public static T Construct<T>(params object[] args) where T : class{ return Activator.CreateInstance( typeof(T), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, args, null) as T;}public static object InvokeMethod(this object o, string methodName, params object[] args){ var mi = o.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance); if (mi == null) throw new ArgumentOutOfRangeException("methodName", string.Format("Method {0} not found", methodName)); return mi.Invoke(o, args);} 这篇关于如何创建一个文件来填充HttpContext.Current.Request.Files?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-26 14:27