The javascript actionresult sets the response.ContentType to application/x-javascript
where as the content actionresult can be set by calling its ContentType property.
Javascript结果:
using System;
namespace System.Web.Mvc
{
public class JavaScriptResult : ActionResult
{
public string Script
{
get;
set;
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = "application/x-javascript";
if (this.Script != null)
{
response.Write(this.Script);
}
}
}
}
ContentResult
public class ContentResult : ActionResult
{
public string Content
{
get;
set;
}
public Encoding ContentEncoding
{
get;
set;
}
public string ContentType
{
get;
set;
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
HttpResponseBase response = context.HttpContext.Response;
if (!string.IsNullOrEmpty(this.ContentType))
{
response.ContentType = this.ContentType;
}
if (this.ContentEncoding != null)
{
response.ContentEncoding = this.ContentEncoding;
}
if (this.Content != null)
{
response.Write(this.Content);
}
}
}
好处是您在MVC代码中明确表示这是JS,并且您的结果是使用正确的ContentType发送到客户端。