簡體   English   中英

ASP.NET MVC 5自定義屬性錯誤

[英]ASP.NET MVC 5 Custom Attribute Error

我在MVC應用程序中實現了圖像上載選項,但始終出現以下錯誤:找不到類型或名稱空間名稱'FileSizeAttribute'。

從下面的代碼中可以看到,我正在實現cusom屬性來確定允許的文件大小和類型。

public class UploadImageModel
    {
        [FileSize(10240)]
        [FileTypes("jpg,jpeg,png")]
        public HttpPostedFileBase File { get; set; }
    }

如您在此處看到的,我在ProfileController.cs中定義了這些屬性。

public class FileSizeAttribute : ValidationAttribute
        {
            private readonly int _maxSize;

            public FileSizeAttribute(int maxSize)
            {
                _maxSize = maxSize;
            }

            public override bool IsValid(object value)
            {
                if (value == null) return true;

                return _maxSize > (value as HttpPostedFile).ContentLength;
            }
            public override string FormatErrorMessage(string name)
            {
                return string.Format("The image size should not exceed {0}", _maxSize);
            }
        }

        public class FileTypesAttribute : ValidateInputAttribute
        {
            private readonly List<string> _types;

            public FileTypesAttribute(string types)
            {
                _types = types.Split(',').ToList();
            }

            public override bool IsValid(object value)
            {
                if (value == null) return true;

                var fileExt = System.IO.Path.GetExtension((value as HttpPostedFile).FileName).Substring(1);
                return _types.Contains(fileExt, StringComparer.OrdinalIgnoreCase);
            }

            public override string FormatErrorMessage(string name)
            {
                return string.Format("Invalid file type. Only the following types {0} are supported.",
                    String.Join(", ", _types));
            }
        }
        [HttpPost]
        public ActionResult PhotoUpload(UploadImageModel imageModel)
        {
            string path = @"D:\Temp\";

            if (ModelState.IsValid)
            {
                if (imageModel != null && imageModel.File != null)
                    image.SaveAs(path + imageModel.File.FileName);

                return RedirectToAction("Profile");
            }

            return View();
        }

我應該提到,由於我是MVC的新手,因此我正在改編本文中的代碼,並且剛剛學習了一些技巧。 也就是說,我是否需要在我的控制器的標頭中包含using子句,還是我遺漏的語法問題? 先謝謝您的幫助。

假設您至少將這些屬性移出了一個組合文件,稱為ExtensionAttributes.cs 在該文件中,您將看到類似

namespace artisan.Attributes
{
    public class FileSizeAttribute : ValidationAttribute
    {
        private readonly int _maxSize;

        public FileSizeAttribute(int maxSize)
        {
            _maxSize = maxSize;
        }

        public override bool IsValid(object value)
        {
            if (value == null) return true;

            return _maxSize > (value as HttpPostedFile).ContentLength;
        }
        public override string FormatErrorMessage(string name)
        {
            return string.Format("The image size should not exceed {0}", _maxSize);
        }
    }

    public class FileTypesAttribute : ValidateInputAttribute
    {
        private readonly List<string> _types;

        public FileTypesAttribute(string types)
        {
            _types = types.Split(',').ToList();
        }

        public override bool IsValid(object value)
        {
            if (value == null) return true;

            var fileExt = System.IO.Path.GetExtension((value as HttpPostedFile).FileName).Substring(1);
            return _types.Contains(fileExt, StringComparer.OrdinalIgnoreCase);
        }

        public override string FormatErrorMessage(string name)
        {
            return string.Format("Invalid file type. Only the following types {0} are supported.",
                String.Join(", ", _types));
        }
    }
}

然后,要在模型上使用屬性,您需要執行以下操作

using artisan.Attributes;

public class UploadImageModel
{
    [FileSize(10240)]
    [FileTypes("jpg,jpeg,png")]
    public HttpPostedFileBase File { get; set; }
}

否則,您需要按artisan.Controllers向模型類添加artisan.Controllers ,但這(給定默認項目模板)可能會引起一些循環引用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM