Hidden Features of ASP.NET [closed]

2019-01-02 21:15发布

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here.

More info: https://stackoverflow.com/faq


There are always features that would be useful in fringe scenarios, but for that very reason most people don't know them. I am asking for features that are not typically taught by the text books.

What are the ones that you know?

标签: asp.net .net
30条回答
Rolldiameter
2楼-- · 2019-01-02 21:51

Two things stand out in my head:

1) You can turn Trace on and off from the code:

#ifdef DEBUG 
   if (Context.Request.QueryString["DoTrace"] == "true")
                {
                    Trace.IsEnabled = true;
                    Trace.Write("Application:TraceStarted");
                }
#endif

2) You can build multiple .aspx pages using only one shared "code-behind" file.

Build one class .cs file :

public class Class1:System.Web.UI.Page
    {
        public TextBox tbLogin;

        protected void Page_Load(object sender, EventArgs e)
        {

          if (tbLogin!=null)
            tbLogin.Text = "Hello World";
        }
    }

and then you can have any number of .aspx pages (after you delete .designer.cs and .cs code-behind that VS has generated) :

  <%@ Page Language="C#"  AutoEventWireup="true"  Inherits="Namespace.Class1" %>
     <form id="form1" runat="server">
     <div>
     <asp:TextBox  ID="tbLogin" runat="server"></asp: TextBox  >
     </div>
     </form>

You can have controls in the ASPX that do not appear in Class1, and vice-versa, but you need to remeber to check your controls for nulls.

查看更多
胡撸娃i
3楼-- · 2019-01-02 21:52

WebMethods.

You can using ASP.NET AJAX callbacks to web methods placed in ASPX pages. You can decorate a static method with the [WebMethod()] and [ScriptMethod()] attributes. For example:

[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();
}

Now, in your ASPX page you can do this:

<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>

And call your server side method via JavaScript using:

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

    function OnGetFruitComplete(result)
    {
        alert("You got fruit: " + result);
    }
</script>
查看更多
笑叹、
4楼-- · 2019-01-02 21:52

HttpModules. The architecture is crazy elegant. Maybe not a hidden feature, but cool none the less.

查看更多
对你真心纯属浪费
5楼-- · 2019-01-02 21:53

HttpContext.Current.IsDebuggingEnabled

This is great for determining which scripts to output (min or full versions) or anything else you might want in dev, but not live.

查看更多
到我身边
6楼-- · 2019-01-02 21:54
  • HttpContext.Current will always give you access to the current context's Request/Response/etc., even when you don't have access to the Page's properties (e.g., from a loosely-coupled helper class).

  • You can continue executing code on the same page after redirecting the user to another one by calling Response.Redirect(url, false )

  • You don't need .ASPX files if all you want is a compiled Page (or any IHttpHandler). Just set the path and HTTP methods to point to the class in the <httpHandlers> element in the web.config file.

  • A Page object can be retrieved from an .ASPX file programmatically by calling PageParser.GetCompiledPageInstance(virtualPath,aspxFileName,Context)

查看更多
Laege°残爷
7楼-- · 2019-01-02 21:54

DefaultButton property in Panels.

It sets default button for a particular panel.

查看更多
登录 后发表回答