这个问题之所以存在,是因为它确实存在 具有历史意义,但事实并非如此 被认为是一个很好的主题问题 因为是本网站,所以请不要使用 作为证据,你可以问类似的问题 这里的问题。 更多信息:https://stackoverflow.com/faq


总有一些功能在边缘场景中很有用,但正是因为这个原因,大多数人都不知道它们。我要求的是课本上通常没有教过的特性。

你知道的是什么?


当前回答

WebMethods。

你可以使用ASP。NET AJAX回调到ASPX页面中的web方法。你可以用[WebMethod()]和[ScriptMethod()]属性来修饰一个静态方法。例如:

[System.Web.Services.WebMethod()] 
[System.Web.Script.Services.ScriptMethod()] 
public static List<string> GetFruitBeginingWith(string letter)
{
    List<string> products = new List<string>() 
    { 
        "Apple", "Banana", "Blackberry", "Blueberries", "Orange", "Mango", "Melon", "Peach"
    };

    return products.Where(p => p.StartsWith(letter)).ToList();
}

现在,在你的ASPX页面你可以这样做:

<form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
        <input type="button" value="Get Fruit" onclick="GetFruit('B')" />
    </div>
</form>

并通过JavaScript调用您的服务器端方法使用:

    <script type="text/javascript">
    function GetFruit(l)
    {
        PageMethods.GetFruitBeginingWith(l, OnGetFruitComplete);
    }

    function OnGetFruitComplete(result)
    {
        alert("You got fruit: " + result);
    }
</script>

其他回答

Page对象上的ClientScript属性。

你知道可以用ASP。IIS或Visual Studio之外的网络?

整个运行时都打包好了,可以在任何想要尝试的进程中托管。使用ApplicationHost, HttpRuntime和HttpApplication类,你也可以打磨这些.aspx页面,并从中得到漂亮的HTML输出。

HostingClass host = ApplicationHost.CreateApplicationHost(typeof(HostingClass), 
                                            "/virtualpath", "physicalPath");
host.ProcessPage(urlToAspxFile); 

你的主持课程:

public class HostingClass : MarshalByRefObject
{
    public void ProcessPage(string url)
    {
        using (StreamWriter sw = new StreamWriter("C:\temp.html"))
        {
            SimpleWorkerRequest worker = new SimpleWorkerRequest(url, null, sw);
            HttpRuntime.ProcessRequest(worker);
        }
                    // Ta-dah!  C:\temp.html has some html for you.
    }
}

我想到了一个特性,有时候你需要隐藏页面的某些部分。你可以用javascript或者下面这段简单的代码:

if (Request.Browser.Crawler){
        HideArticleComments();

在开始一个长时间运行的任务之前,检查客户端是否仍然连接:

if (this.Response.IsClientConnected)
{
  // long-running task
}

零售模式在机器。配置水平:

<configuration>
  <system.web>
    <deployment retail="true"/>
  </system.web>
</configuration>

覆盖网络。配置设置以强制调试为false,打开自定义错误并禁用跟踪。不再忘记在发布之前更改属性—只需将它们全部配置为开发或测试环境,并更新生产零售设置。