javascript actionresult 将 response.ContentType 设置为 application/x-javascript,其中 content actionresult 可以通过调用其 ContentType 属性来设置。
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);
}
}
}
}
内容结果
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 的客户端。