ContentResult vs. string

后端 未结 6 1425
-上瘾入骨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:35

    You can't return a string on a method which returns an ActionResult, so this is when you could use a ContentResult to return a plain string like so:

    public ContentResult Hello()
    {
        return Content("hello world!");
    }
    

    ContentResult by default returns a type of text/plain. If you only ever needed to return a string, then you would use the method to return a string

提交回复
热议问题