0

これは私のビューの一部です:

<img src="@{Html.RenderAction("GetUserProfileImage", "Home", new { area = "" });}" alt="" />

そして私のコントローラー:

    public ActionResult GetUserProfileImage() {

        string defaultresult = "http://MyProject.dev/Content/img/sample_user.png";
        if (Member.LogIn()) {

            DBFile file = GetMemberPicFromDB();
            return File(file.Content, file.ContentType);

        } else {
            return Content(defaultresult);
        }
    }

したがって、DBからファイルを取得する場合はすべて問題ありませんが、URL(by return Content)を返すと、レンダリングされたhtmlに次のような悪い結果が生じます。

<imghttp: myproject.dev="" content="" img="" sample_user.png="" src="" alt="">

    <div>
        some content from outside of img tag added here
    </div>
</imghttp:>

また、ビューを次のように変更します。

<img src="http://myproject.dev/Content/img/sample_user.png" alt="" />

そして、すべてがOKです、

では、どこに問題があるのでしょうか。コンテンツは単純な文字列を属性に返すことができないようです。では、単純srcな文字列を返すための提案は何ですか?

それが不可能な場合

DBから返されるときにURL(http://myproject.dev/Content/img/sample_user.png)などでファイルを取得すると思いreturn File()ますが、このURl()でコントローラーにファイルを取得するにはどうすればよいhttp://myproject.dev/Content/img/sample_user.pngですか?

そしてあなたが持っているなら他の提案はありますか?

4

1 に答える 1

1

どちらの場合もコントローラー アクションからファイルを返し、次のようにイメージでこのアクションを参照する必要があります。

<img src="@Url.Action("GetUserProfileImage", "Home", new { area = "" })" alt="" />

次に、コントローラーアクションで、両方のケースで File 結果を返します。

public ActionResult GetUserProfileImage() 
{
    if (Member.LogIn()) 
    {
        DBFile file = GetMemberPicFromDB();
        return File(file.Content, file.ContentType);
    }
    else 
    {
        string image = Server.MapPath("~/content/img/sample_user.png");
        return File(image, "image/png");
    }
}
于 2012-11-17T13:10:09.377 に答える