English 中文(简体)
在MVC3中,“JavaScript”和“Content”函数之间有什么区别?
原标题:In MVC3, what is the difference between the "JavaScript" and "Content" functions?

在ASP.NET MVC3中,以下两个方法似乎返回相同的结果:

public ActionResult Blah()
{
    return JavaScript("alert( " + DateTime.Now + " );");
}

public ActionResult Blah()
{
    return Content("alert( " + DateTime.Now + " );");
}

但是,当我在Google Chrome中查看第一个的结果时,字体是Mono-Spaced字体,而第二个是Arial(或其他什么)。

这让我相信,可能有一个标题“content-type”为“text/javascript”或其他什么。。。

我的问题是:

  • “JavaScript”函数(产生JavaScriptResult)做了什么而Content方法(产生ContentResult)没有做什么

  • 这种方法有什么好处?

请不要将宗教原因包括在内,说明为什么这种方法“不好”。。。我只关心知道“什么”。。。如“它做什么?”

最佳回答

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发送到客户端。

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.