首页 > 解决方案 > 为什么iis使用asp net mvc控制器文件流返回403错误代码

问题描述

我有一个控制器有两种方法

public ActionResult Index1(string c, string d)
    {
        string path = "some path";

        if (System.IO.File.Exists(path))
        {
            var bytes = System.IO.File.ReadAllBytes(path);
            return base.File(bytes, MimeMapping.GetMimeMapping(path));
        }

        path = "/assets/images/no.jpg";
        return File(path, "application/jpeg", "no.jpg");
    }
public ActionResult Index2(string c, string d)
    {
        string path = "some path";
        string _nomefile = "some name";
        string _mime = "";
        if (System.IO.File.Exists(path))
        {
            return new FileStreamResult(new FileStream(path, FileMode.Open), _mime) { FileDownloadName = _nomefile};

        }
        path = "/assets/images/no.jpg";
        return File(path, "application/jpeg", "no.jpg");
    }

这被这样的视图使用

@foreach (var item in Model.OrderBy(x => x.Titolo))
        {
            <div class="col-md-3 col-sm-12  immagini margin-bottom-20">
                <a class="image-hover" href="/Index1?c=@item.Immagine&d=N" data-toggle="tooltip" title="@item.Titolo - @item.Descrizione">
                    <img src="/Index1?c=@item.Immagine&d=M" alt="@item.Titolo"><br />
                    <span>@item.Descrizione</span>
                </a>
            </div>
        }

问题是 IIS 使用这两种方法都返回一个间歇性的 403 禁止,这完全是随机的。在包含 30 个图像的列表中,不显示 3 或 4 个。我没有实施任何身份验证,所有文件都在同一台机器上。

标签: c#asp.netasp.net-mvciis-8.5

解决方案


好的,这是我的解决方案。

有必要在 IIS 8.5 中设置下限

https://serverfault.com/a/908740


推荐阅读