我正在尝试将一些C#
MVC 遗留代码移动到共享 DLL 中。到目前为止一切顺利,但有人问我该共享 DLL 不需要System.Web
以任何方式引用。
System.Web 的 DLL 中使用的唯一类型是HttpPostedFileBase
:
public string ChangeAttachment(int userId, HttpPostedFileBase file)
{
string attachmentsFolderPath = ConfigurationManager.AppSettings["AttachmentsDirectory"];
if (!Directory.Exists(attachmentsFolderPath))
{
Directory.CreateDirectory(attachmentsFolderPath);
}
string fileTarget = Path.Combine(attachmentsFolderPath, userId.ToString() + Path.GetExtension(file.FileName));
if (File.Exists(fileTarget))
{
File.Delete(fileTarget);
}
file.SaveAs(fileTarget);
return fileTarget;
}
如您所见,这里不需要 HTTP 或 Web 功能,因为只使用了它的FileName
和SaveAs()
成员。
是否有一个替代品可以在调用者HttpPostedFileBase
处轻松转换为它,以便我需要作为参数传递的只是一个非 Web 文件?
注意:HttpPostedFileBase
直接继承自System.Object
,而不是任何文件类。