C# MVC图像URL下载图像,而不是显示视图

C# MVC图像URL下载图像,而不是显示视图,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,新手到MVC。我试图使用MVC在浏览器中显示图形,但当我运行应用程序时,页面将下载图像并关闭 我的控制器 public ActionResult DrawChart() { var chart = new Chart(width: 300, height: 200) .AddSeries( chartType: "bar", xValue: new[]

新手到
MVC
。我试图使用
MVC
在浏览器中显示图形,但当我运行应用程序时,页面将下载图像并关闭

我的控制器

 public ActionResult DrawChart()
    {
        var chart = new Chart(width: 300, height: 200)
            .AddSeries(
                        chartType: "bar",
                        xValue: new[] { "10 Records", "20 Records", "30 Records", "40 Records" },
                        yValues: new[] { "50", "60", "78", "80" })
                        .GetBytes("png");

        return File(chart, "image/bytes");
    }
我的观点

    @{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>DrawChart</title>
</head>
<body>
    <div> 
        <img src="@Url.Action("DrawChart")" alt="Drawing chart with HTML Helper" />
    </div>
</body>
</html>
@{
布局=空;
}
挂图

您正在返回mime类型为
image/bytes
的文件结果,并且您还正在调用
GetBytes(“png”)
方法(我假设该方法正在获取png字节)


在这种情况下,您可以尝试
返回文件(图表,“image/png”)
这应该指示浏览器显示图像。

您返回的是mime类型为
image/bytes
的文件结果,并且您还调用了
GetBytes(“png”)
方法(我假设是获取png字节)


在这种情况下,您可以尝试
返回文件(图表,“image/png”)这将指示浏览器显示图像。

您可以使用Razor将图像直接写入视图,如下图所示,或者使用“局部视图”(Partial View)并根据需要渲染图像

    <div>
@{
    var chart = new Chart(width: 300, height: 200)
                .AddSeries(
                            chartType: "bar",
                            xValue: new[] { "10 Records", "20 Records", "30 Records", "40 Records" },
                            yValues: new[] { "50", "60", "78", "80" })
                            .Write();
    }
</div>

@{
var图表=新图表(宽度:300,高度:200)
.AddSeries(
图表类型:“条”,
xValue:new[]{“10条记录”、“20条记录”、“30条记录”、“40条记录”},
Y值:新[]{“50”、“60”、“78”、“80”})
.Write();
}

请参阅此链接:

您可以使用Razor将其直接写入视图,如下图所示,或者使用“局部视图”并在任何需要的地方进行渲染

    <div>
@{
    var chart = new Chart(width: 300, height: 200)
                .AddSeries(
                            chartType: "bar",
                            xValue: new[] { "10 Records", "20 Records", "30 Records", "40 Records" },
                            yValues: new[] { "50", "60", "78", "80" })
                            .Write();
    }
</div>

@{
var图表=新图表(宽度:300,高度:200)
.AddSeries(
图表类型:“条”,
xValue:new[]{“10条记录”、“20条记录”、“30条记录”、“40条记录”},
Y值:新[]{“50”、“60”、“78”、“80”})
.Write();
}

请参阅此链接:

谢谢。答案和参考非常有用谢谢,如果您将其标记为对其他人非常有用的答案,将不胜感激。答案和参考资料非常有用谢谢,如果您将其标记为对其他人非常有用的答案,将不胜感激