ContentResult vs. string

后端 未结 6 1427
-上瘾入骨i
-上瘾入骨i 2020-12-29 01:37

I recently was asked why to use ContentResult instead of returning string. Unfortunately I could not give a better answer than: \"It is best practi

6条回答
  •  心在旅途
    2020-12-29 02:15

    Two main advantages:

    • You can specify the content encoding via the ContentEncoding property
    • You can specify the content type (ie, 'text/html') via the ContentType property

    Also, if you want to be OO-clean about it, ContentResult, along with all ActionResult derived classes follow the Command Pattern by providing an ExecuteResult command for the MVC pipeline to run.

    UPDATE: 19-Nov-2020 The original Command Pattern link (dead link) was no longer working so I updated it to reference a similar Microsoft document. Also, to see a great explanation on the Command Pattern, view this link.

提交回复
热议问题