简体   繁体   English

ContentResult 与字符串

[英]ContentResult vs. string

I recently was asked why to use ContentResult instead of returning string .最近有人问我为什么要使用ContentResult而不是返回string Unfortunately I could not give a better answer than: "It is best practice."不幸的是,我无法给出比“这是最佳实践”更好的答案。

Does anyone have a better answer?有人有更好的答案吗?

To better understand the question.为了更好地理解这个问题。 What's the difference?有什么不同?

public ActionResult Foo(){
    return Content("Some string");
}

public string Bar(){
    return "Some string";
}

If you return something other than an ActionResult the default behavior is to create a ContentResult wrapping the result of calling ToString() on whatever you did return (or EmptyResult if you returned null ).如果您返回除ActionResult以外的其他内容,则默认行为是创建一个ContentResult将调用ToString()的结果包装在您返回的任何内容上(如果返回null EmptyResult )。 Reasons I can think of to explicitly return ContentResult :我能想到的明确返回ContentResult

  • It reinforces the fact that the method is an action, rather than a regular method, so devs are less likely to make mistakes like casually renaming it.它强化了这样一个事实,即方法是一个动作,而不是常规方法,因此开发人员不太可能犯错误,比如随意重命名它。
  • If in the future you need to specify the content-type you won't need to change the method signature.如果将来您需要指定内容类型,则不需要更改方法签名。
  • It doesn't hide the ToString() call.它不会隐藏ToString()调用。 This doesn't matter if you're returning string , but returning a complex type could have unexpected results.如果您返回string ,这无关紧要,但返回复杂类型可能会产生意外结果。

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:您不能在返回 ActionResult 的方法上返回字符串,因此此时您可以使用 ContentResult 返回一个纯字符串,如下所示:

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

ContentResult by default returns a type of text/plain . ContentResult 默认返回一种text/plain类型。 If you only ever needed to return a string, then you would use the method to return a string如果您只需要返回一个字符串,那么您将使用该方法返回一个字符串

Two main advantages:两大优势:

  • You can specify the content encoding via the ContentEncoding property您可以通过ContentEncoding属性指定内容编码
  • You can specify the content type (ie, 'text/html') via the ContentType property您可以通过ContentType属性指定内容类型(即“text/html”)

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.此外,如果您想对它保持面向对象的清洁,那么ContentResult以及所有ActionResult派生类都遵循命令模式,为 MVC 管道提供ExecuteResult命令以运行。

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.更新:2020 年 11 月 19 日原始命令模式链接( 死链接)不再有效,因此我更新了它以引用类似的 Microsoft 文档。 Also, to see a great explanation on the Command Pattern, view this link .此外,要查看有关命令模式的精彩解释,请查看此链接

Returning a ContentResult helps protect your application.返回 ContentResult 有助于保护您的应用程序。

Eg If your application allows input from users, a malicious user might try to parse a javascript text.例如,如果您的应用程序允许来自用户的输入,恶意用户可能会尝试解析 javascript 文本。 This will prevent javascript from executing in your application这将阻止 javascript 在您的应用程序中执行

One difference as i know is :我所知道的一个区别是:

Content return result without quotations marks if you want.如果需要,内容返回结果不带引号。

String return result in Quotations marks all the time.字符串返回结果一直在引号中。

One another difference is Content result can return different content's result.另一个区别是内容结果可以返回不同内容的结果。 Like string, Html, JavaScript result etc.如字符串、Html、JavaScript 结果等。

But string return only the string result.但字符串只返回字符串结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM