多くの場合、ASP.NET アプリケーションの任意の部分から、複数の要求にわたって同じ情報にアクセスする必要があります。以下の例を使用して、現在のリクエストの URL を取得しようとしています (必要なプロパティをよく忘れてしまいます)。ASP.NET が HttpContext を処理する方法に似たアプローチを考え出そうとしています。これまでのところ、私は次のことを思いつきました:
public interface ICustomContext {
HttpContextBase Http { get; }
string Url { get; }
}
public class CustomContext : ICustomContext {
private readonly HttpContextBase _httpContext;
public CustomContext(HttpContextBase httpContext) {
_httpContext = httpContext;
}
public HttpContextBase Http {
get { return _httpContext; }
}
public string Url {
get { return Http.Request.Url.AbsoluteUri; }
}
}
public class MyContext {
private static ICustomContext _instance = new CustomContext(new HttpContextWrapper(HttpContext.Current));
public static ICustomContext Current {
get { return _instance; }
}
}
これにより、後で独自のコンテキストの上に追加のプロパティを追加できます。私は簡単に言うことができればいいのにと思いました:
MyContext.Current.Url;
ただし、最初に呼び出されたときは常にページの URL を返します。これはある種のスレッドの問題だと思いますが、解決方法がわかりません。
助けていただければ幸いです。ああ、理想的には、クリーンで簡単にテストできるソリューションが欲しいことに注意してください。
ありがとう