How to serve html file from another directory as ActionResult

前端 未结 6 1620
长发绾君心
长发绾君心 2020-12-05 13:38

I have a specialised case where I wish to serve a straight html file from a Controller Action.

I want to serve it from a different folder other than the Views folder

6条回答
  •  一生所求
    2020-12-05 13:48

    I extended wahid's answer to create HtmlResult

    Create Html Result which extends FilePathResult

    public class HtmlResult : FilePathResult
    {
        public HtmlResult(string path)
            : base(path, "text/html")
        {
        }
    }
    

    Created static method on controller

    public static HtmlResult Html(this Controller controller, string path)
    {
        return new HtmlResult(path);
    }
    

    used like we return view

    public HtmlResult Index()
    {
        return this.Html("~/Index.html");
    }
    

    Hope it helps

提交回复
热议问题