简体   繁体   English

aspx页面的全局功能

[英]Global function from aspx page

I have a function Static1Url in a .cs which does a job of pre-pending a URL string: 我在.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);
        }
    }
}

I'd like to be able to access this from an aspx page like so: 我希望能够从这样的aspx页面访问它:

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

At the moment, to be able to do this I must use the following code: 目前,为了能够这样做,我必须使用以下代码:

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

If I add an Import statement at the top of the page ( <%@ Import Namespace="Website2012" %> ) then I can now use the following: 如果我在页面顶部添加一个Import语句( <%@ Import Namespace="Website2012" %> ),那么我现在可以使用以下内容:

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

Call me lazy but I'd prefer an even simpler solution, preferably one that didn't involve an Import at the top of the page but then didn't mean I had to use a lengthy function call. 叫我懒惰,但我更喜欢一个更简单的解决方案,最好是一个不涉及页面顶部的导入,但这并不意味着我不得不使用冗长的函数调用。 Is this possible? 这可能吗?

EDIT (For the benefit of Imran Balouch) 编辑(为Imran Balouch的利益)

I have come up with this idea for handling multiple page types, is this good practice: 我已经提出了处理多种页面类型的想法,这是一个很好的做法:

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

No its not. 不,这不对。 You need the class name atleast to access the static method (if you include the namespace above). 您至少需要类名才能访问静态方法(如果您在上面包含命名空间)。 If you don't include the namespace then you have to access it via the namespace like in your example. 如果您不包含命名空间,则必须通过命名空间访问它,如示例中所示。

Website2012.GlobalSiteFunctions.Static1Url

I think the answer is 'Yes' and 'No' depending on exactly what you are really wanting. 我认为答案是'是'和'否'取决于你真正想要的是什么。 I have 2 considerations. 我有两个注意事项。 I recommend against the first in the situation that is presented in the question, but this is useful for very similar situations and this 1st option does more directly answer the question as asked but I believe the 2nd is a better answer as to what I believe is really desired. 我建议在问题中提出的情况下反对第一个,但这对于非常类似的情况很有用,第一个选项更直接地回答了问题但我相信第二个是更好的答案我认为是什么真的很想要。

1: You can use an extension method on the System.Web.UI.Page so you can call your method by: this.Static1Url(...) . 1:您可以在System.Web.UI.Page上使用扩展方法,以便您可以通过以下方式调用您的方法: this.Static1Url(...) Your extension method would look something like this: 您的扩展方法如下所示:

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

But because you don't use that variable p in your method, and it's really not an intended use of an extension method, I recommend against this usage. 但是因为你没有在你的方法中使用那个变量p ,并且它实际上不是扩展方法的预期用途,我建议不要使用这种用法。 I wud call this kind of use 'slop'. 我称之为“slop”。 But a good method that I use this for is this extension method: 但是我使用它的一个好方法是这个扩展方法:

    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 = "?")

The Request collection needs the Page instance, so this example is quite applicable. Request集合需要Page实例,因此这个例子非常适用。 And calling it is simple from inside a web page: this.RequestFQ("myKey"); 从网页内部调用它很简单: this.RequestFQ("myKey"); .

Now for what I think is your best solution: 现在我觉得这是你最好的解决方案:

2: It seems to me that your real question is you making your call a shorter name so you don't have all your lengthy 'GlobalSiteFunctions.Static1Url()'. 2:在我看来,你真正的问题是你的呼叫是一个较短的名字,所以你没有你所有冗长的'GlobalSiteFunctions.Static1Url()'。 I remind you (or enlighten you to the fact) that a using statement can rename your long class name so Website2012.GlobalSiteFunctions.Static1Url(...) can be called with g.Static1Url(...) with a using statement such as using g = Website2012.GlobalSiteFunctions; 我提醒你(或启发你的事实),一个using语句可以重命名你长的类名,以便Website2012.GlobalSiteFunctions.Static1Url(...)可以调用g.Static1Url(...)using的语句,如using g = Website2012.GlobalSiteFunctions; .

So your questions specifically asks whether you can avoid the OOP design, and technically, no, you cannot. 所以你的问题特别询问你是否可以避免OOP设计,从技术上讲,不,你不能。 But you can shorten the length of characters you need to use to call your method by this 2nd option I provide. 但您可以通过我提供的第二个选项缩短您需要用来调用方法的字符长度。 It seems to me that this is really what you are wanting. 在我看来,这真的是你想要的。

Happy coding! 快乐的编码!

The solution for this that can come in my mind is override the Page Class and in that overridden Page Class, write down your function and inherit every page of yours from the derived class, this way you can use it without using the import statement. 我想到的解决方案是覆盖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);
    }
}

than your page must be like: 比你的页面必须像:

public partial class YourPage : MyPage
{

}

and you can use it like: 你可以像以下一样使用它:

<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