簡體   English   中英

aspx頁面的全局功能

[英]Global function from aspx page

我在.cs中有一個函數Static1Url ,它可以預先處理一個URL字符串:

namespace Website2012
{
    public static class GlobalSiteFunctions
    {

        /// <summary>Returns a formatted URL mapping to Static1 on a CDN.</summary>
        /// <param name="url">The URL to be formatted.</param>
        /// <returns>Formatted string.</returns>
        public static string Static1Url(string url)
        {
            return string.Format("{0}{1}", ConfigurationManager.AppSettings["Static1CDNUrl"], url);
        }
    }
}

我希望能夠從這樣的aspx頁面訪問它:

<p>This is the complete URL: <%= Static1Url("my-file-name.jpg") %></p>

目前,為了能夠這樣做,我必須使用以下代碼:

<p>This is the complete URL: <%= Website2012.GlobalSiteFunctions.Static1Url("my-file-name.jpg") %></p>

如果我在頁面頂部添加一個Import語句( <%@ Import Namespace="Website2012" %> ),那么我現在可以使用以下內容:

<p>This is the complete URL: <%= GlobalSiteFunctions.Static1Url("my-file-name.jpg") %></p>

叫我懶惰,但我更喜歡一個更簡單的解決方案,最好是一個不涉及頁面頂部的導入,但這並不意味着我不得不使用冗長的函數調用。 這可能嗎?

編輯(為Imran Balouch的利益)

我已經提出了處理多種頁面類型的想法,這是一個很好的做法:

public class MyPage : System.Web.UI.Page
{
    public static bool Test()
    {
        return Functions.Test();
    }
}

public class MyUserControl : System.Web.UI.UserControl
{
    public static bool Test()
    {
        return Functions.Test();
    }
}


private class Functions
{
    public static bool Test()
    {
        return true;
    }
}

不,這不對。 您至少需要類名才能訪問靜態方法(如果您在上面包含命名空間)。 如果您不包含命名空間,則必須通過命名空間訪問它,如示例中所示。

Website2012.GlobalSiteFunctions.Static1Url

我認為答案是'是'和'否'取決於你真正想要的是什么。 我有兩個注意事項。 我建議在問題中提出的情況下反對第一個,但這對於非常類似的情況很有用,第一個選項更直接地回答了問題但我相信第二個是更好的答案我認為是什么真的很想要。

1:您可以在System.Web.UI.Page上使用擴展方法,以便您可以通過以下方式調用您的方法: this.Static1Url(...) 您的擴展方法如下所示:

public static string Static1Url(this System.Web.UI.Page p, string url)
{   return string.Format("{0}{1}", ConfigurationManager.AppSettings["Static1CDNUrl"], url);
}

但是因為你沒有在你的方法中使用那個變量p ,並且它實際上不是擴展方法的預期用途,我建議不要使用這種用法。 我稱之為“slop”。 但是我使用它的一個好方法是這個擴展方法:

    public static string RequestFQ(this System.Web.UI.Page p, string key, string valueForNullResult = "?")
    {   string v = p.Request.Form[key] ?? p.Request.QueryString[key];
        if (v == valueForNullResult) { v = null; }
        return v;
    } // public string RequestFQ(this System.Web.UI.Page p, string key, string valueForNullResult = "?")

Request集合需要Page實例,因此這個例子非常適用。 從網頁內部調用它很簡單: this.RequestFQ("myKey");

現在我覺得這是你最好的解決方案:

2:在我看來,你真正的問題是你的呼叫是一個較短的名字,所以你沒有你所有冗長的'GlobalSiteFunctions.Static1Url()'。 我提醒你(或啟發你的事實),一個using語句可以重命名你長的類名,以便Website2012.GlobalSiteFunctions.Static1Url(...)可以調用g.Static1Url(...)using的語句,如using g = Website2012.GlobalSiteFunctions;

所以你的問題特別詢問你是否可以避免OOP設計,從技術上講,不,你不能。 但您可以通過我提供的第二個選項縮短您需要用來調用方法的字符長度。 在我看來,這真的是你想要的。

快樂的編碼!

我想到的解決方案是覆蓋Page Class,在覆蓋的Page Class中,記下你的函數並從派生類繼承你的每一頁,這樣你就可以在不使用import語句的情況下使用它。

public class MyPage : System.Web.UI.Page
{
    protected string Static1Url(string url)
    {
        return string.Format("{0}{1}", ConfigurationManager.AppSettings["Static1CDNUrl"], url);
    }
}

比你的頁面必須像:

public partial class YourPage : MyPage
{

}

你可以像以下一樣使用它:

<p>This is the complete URL: <%= base.Static1Url("my-file-name.jpg") %></p>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM