where should i encode this html data in an asp.net mvc site

蓝咒 提交于 2020-01-07 03:53:13

问题


here is my view code:

<%=Model.HtmlData %>

here is my controller code:

    public ActionResult GetPage()
    {
        ContentPageViewModel vm = new ContentPageViewModel();
        vm.HtmlData = _htmlPageRepository.Get("key");
        return View(vm);
    }

my repository class basically queries a database table that has the fields:

id, pageName, htmlContent

the .Get() method passes in a pageName (or key) and returns the htmlContent value.

Right now i have just started this (haven't persisted anything to the db yet) so i am not doing any explicit encoding in my code now.

What is the best practice for where i need to do encoding (in the model, the controller, the view ??)


回答1:


Encoding is a concern of the view. You may have two very different displays using the same database, so often it isn't advisable to store the data in a state required by the specific view.

As a side note... If you are using .NET 4

<%: Model.HtmlData %>

Is the new

<%= Sever.HtmlEncode(Model.HtmlData) %>


来源:https://stackoverflow.com/questions/2914062/where-should-i-encode-this-html-data-in-an-asp-net-mvc-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!