.net framework 2.0 upgrade to .net framework 4.0 problem urgent
-
Saturday, February 25, 2012 12:43 AMWe are really need you help, our company have two project IDE used VS 2010 target .NET 2.0 framework asp.net web project.
yesterday we have upgrade this two web project from .net framework 2.0 to .net framework 4.0. Now it runing very slow. open websitespend long time or timeout, it led to this two website can not work now.Our team have check out project and try to solve it, but it can find problem until now. We have try all kinds of way also can not solve thisproblem,at last we have remove one httpmodel, this two web project runing is ok,, If we remove this httpmodel, the project will lost some fuctionand customer booking order data is not complete. this httpmodel can not remove. so we ask you help how to solve this case,,post simple cod as below ,
asp.net windows phone sivlerlght
- Moved by Lie You Thursday, March 01, 2012 2:05 AM ASP.Net related. (From:Visual C# General)
All Replies
-
Saturday, February 25, 2012 12:50 AMusing System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.SessionState;
using TongCheng.Mobile.Framework.Business;
namespace TongCheng.Mobile.Wap
{
/// <summary>
///
/// </summary>
public class UrlAppendParams : System.Web.IHttpModule
{
private long refId = 0;
private string authCode = string.Empty;
public void Init(HttpApplication application)
{
application.AcquireRequestState += new EventHandler(Application_AcquireRequestState);
application.PostAcquireRequestState += new EventHandler(Application_PostAcquireRequestState);
application.PostMapRequestHandler += new EventHandler(Application_PostMapRequestHandler);
application.PostRequestHandlerExecute += new EventHandler(Application_ApplyUrlFilter);
}
#region Application_AcquireRequestState
private void Application_AcquireRequestState(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
this.refId = this.GetRefIdByRequest();
if (this.refId > 0)
{
UnionInfo unionInfo = new UnionInfo(this.refId);
unionInfo.AddToSession(SessionKey.UnionInfo);
}
this.authCode = this.GetAuthCodeByRequest();
if (!string.IsNullOrEmpty(this.authCode))
{
AuthenticationInfo authInfo = new AuthenticationInfo(this.authCode);
authInfo.AddToSession(SessionKey.AuthenticationInfo);
}
}
#endregion
#region Application_PostAcquireRequestState
private void Application_PostAcquireRequestState(object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
UrlParamsFilterHttpHandler resourceHttpHandler = HttpContext.Current.Handler as UrlParamsFilterHttpHandler;
if (resourceHttpHandler != null)
{
// set the original handler back
HttpContext.Current.Handler = resourceHttpHandler.OriginalHandler;
}
//// -> at this point session state should be available
//Debug.Assert(application.Session != null, "it did not work :(");
#region REFID
if (this.refId < 1)
{
this.refId = this.GetRefIdByRequest();
}
UnionInfo unionInfo = null;
if (this.refId > 0)
{
unionInfo = new UnionInfo(this.refId);
unionInfo.AddToSession(SessionKey.UnionInfo);
}
else
{
unionInfo = UnionInfo.GetCurrentUnionInfo();
}
if (HttpContext.Current != null && unionInfo != null)
{
if (!HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)))
HttpContext.Current.Items.Add(SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo), unionInfo);
else
HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)] = unionInfo;
}
#endregion
#region AUTHCODE
if (String.IsNullOrEmpty(this.authCode))
{
this.authCode = this.GetAuthCodeByRequest();
}
AuthenticationInfo authInfo = null;
if (!string.IsNullOrEmpty(this.authCode))
{
authInfo = new AuthenticationInfo(this.authCode);
authInfo.AddToSession(SessionKey.AuthenticationInfo);
}
else
{
authInfo = AuthenticationInfo.GetCurrentAuthenticationInfo();
}
if (HttpContext.Current != null && authInfo != null)
{
if (!HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)))
HttpContext.Current.Items.Add(SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo), authInfo);
else
HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)] = authInfo;
}
#endregion
}
#endregion
#region Application_PostMapRequestHandler
private void Application_PostMapRequestHandler(object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
if (application.Context.Handler is IReadOnlySessionState || application.Context.Handler is IRequiresSessionState)
{
// no need to replace the current handler
return;
}
// swap the current handler
application.Context.Handler = new UrlParamsFilterHttpHandler(application.Context.Handler);
}
#endregion
#region Application_ApplyUrlFilter
private void Application_ApplyUrlFilter(Object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
application.Response.Filter = new UrlAppendParamsFilter(application.Response.Filter);
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <returns></returns>
private long GetRefIdByRequest()
{
long refId = 0;
if (!string.IsNullOrEmpty(HttpContext.Current.Request["refid"])
&& Validator.IsInt64(HttpContext.Current.Request["refid"]))
{
refId = Int64.Parse(HttpContext.Current.Request["refid"]);
}
else if (!string.IsNullOrEmpty(HttpContext.Current.Request["unionId"])
&& Validator.IsInt64(HttpContext.Current.Request["unionId"]))
{
refId = Int64.Parse(HttpContext.Current.Request["unionId"]);
}
return refId;
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <returns></returns>
private string GetAuthCodeByRequest()
{
string auth = string.Empty;
if (!string.IsNullOrEmpty(HttpContext.Current.Request["auth"]))
{
auth = HttpContext.Current.Request["auth"];
}
return auth;
}
#endregion
public void Dispose()
{
}
#region CLASS UrlAppendParamsFilter
private class UrlAppendParamsFilter : Stream
{
private Stream Sink { get; set; }
private long _position;
private System.Text.StringBuilder oOutput = new StringBuilder();
public UrlAppendParamsFilter(Stream sink)
{
Sink = sink;
}
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override long Length
{
get { return 0; }
}
public override long Position
{
get { return _position; }
set { _position = value; }
}
public override long Seek(long offset, System.IO.SeekOrigin direction)
{
return Sink.Seek(offset, direction);
}
public override void SetLength(long length)
{
Sink.SetLength(length);
}
public override void Close()
{
Sink.Close();
}
public override void Flush()
{
Sink.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
return Sink.Read(buffer, offset, count);
}
public override void Write(byte[] buffer, int offset, int count)
{
string authCode = UrlAppendParamsFilter.GetAuthCodeByRequest();
long refId = UrlAppendParamsFilter.GetRefIdByRequest();
AuthenticationInfo authInfo = null;
UnionInfo unionInfo = null;
if (HttpContext.Current != null)
{
if (HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)))
authInfo = HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)] as AuthenticationInfo;
if (HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)))
unionInfo = HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)] as UnionInfo;
}
if ((refId <= 0 && unionInfo == null)
&& (string.IsNullOrEmpty(authCode) && authInfo == null))
{
Sink.Write(buffer, 0, buffer.Length);
return;
}
string content = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);
Regex regex = new Regex(RegexResource.HREF, RegexOptions.IgnoreCase);
Regex regexs = new Regex(RegexResource.HREFS, RegexOptions.IgnoreCase);
Regex action_regex = new Regex(RegexResource.ACTION, RegexOptions.IgnoreCase);
if (regex.IsMatch(content))
{
content = Regex.Replace(content, RegexResource.HREF, new MatchEvaluator(ReplaceParams), RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
if (regexs.IsMatch(content))
{
content = Regex.Replace(content, RegexResource.HREFS, new MatchEvaluator(ReplaceParams), RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
if (action_regex.IsMatch(content))
{
content = Regex.Replace(content, RegexResource.ACTION, new MatchEvaluator(ReplaceParams), RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(content);
Sink.Write(data, 0, data.Length);
}
public static string ReplaceParams(Match match)
{
string result = string.Empty;
if (match.Value.StartsWith("http", StringComparison.CurrentCultureIgnoreCase)
|| match.Value.StartsWith("wtai://", StringComparison.CurrentCultureIgnoreCase)
|| match.Value.EndsWith(".css", StringComparison.CurrentCultureIgnoreCase)
|| match.Value.EndsWith(".js", StringComparison.CurrentCultureIgnoreCase)
|| match.Value.EndsWith(".png", StringComparison.CurrentCultureIgnoreCase)
|| match.Value.EndsWith(".jpg", StringComparison.CurrentCultureIgnoreCase)
|| match.Value.EndsWith(".gif", StringComparison.CurrentCultureIgnoreCase))
{
result = match.Value;
}
else
{
#region
long refId = UrlAppendParamsFilter.GetRefIdByRequest();
if (refId <= 0)
{
UnionInfo unionInfo = null;
if (HttpContext.Current != null)
{
if (HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)))
unionInfo = HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)] as UnionInfo;
}
if (unionInfo != null)
{
refId = unionInfo.UnionId;
}
}
#endregion
#region
string authCode = UrlAppendParamsFilter.GetAuthCodeByRequest();
if (string.IsNullOrEmpty(authCode))
{
AuthenticationInfo authInfo = null;
if (HttpContext.Current != null)
{
if (HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)))
authInfo = HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)] as AuthenticationInfo;
}
if (authInfo != null)
{
authCode = authInfo.AuthCode;
}
}
#endregion
result = match.Value;
if (!string.IsNullOrEmpty(authCode) && result.IndexOf("auth=") == -1)
{
if (result.IndexOf('?') == -1)
{
result = result.Insert(result.Length, "?auth=" + authCode);
}
else
{
result = result.Insert(result.Length, "&auth=" + authCode);
}
}
if (refId > 0 && result.IndexOf("refid=") == -1)
{
if (result.IndexOf('?') == -1)
{
result = result.Insert(result.Length, "?refid=" + refId);
}
else
{
result = result.Insert(result.Length, "&refid=" + refId);
}
}
}
return result;
}
public class RegexResource
{
public static readonly string HREF = @"(?<=href=')(?<Link>.*?)(?=')";
public static readonly string HREFS = @"(?<=href=\"")(?<Link>.*?)(?=\"")";
//public static readonly string ACTION = "(<FORM.*ACTION=\")([^\"]*)(\"[^>]*>)";
public static readonly string ACTION = @"(?<=<\s*form\s.*?action\s*="").*?(?="".*?>)";
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private static string GetAuthCodeByRequest()
{
string auth = string.Empty;
if (!string.IsNullOrEmpty(HttpContext.Current.Request["auth"]))
{
auth = HttpContext.Current.Request["auth"];
}
return auth;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private static long GetRefIdByRequest()
{
long refId = 0;
if (!string.IsNullOrEmpty(HttpContext.Current.Request["refid"])
&& Validator.IsInt64(HttpContext.Current.Request["refid"]))
{
refId = Int64.Parse(HttpContext.Current.Request["refid"]);
}
else if (!string.IsNullOrEmpty(HttpContext.Current.Request["unionId"])
&& Validator.IsInt64(HttpContext.Current.Request["unionId"]))
{
refId = Int64.Parse(HttpContext.Current.Request["unionId"]);
}
return refId;
}
}
#endregion
}
// a temp handler used to force the SessionStateModule to load session state
public class UrlParamsFilterHttpHandler : IHttpHandler, IRequiresSessionState
{
internal readonly IHttpHandler OriginalHandler;
public UrlParamsFilterHttpHandler()
{
OriginalHandler = HttpContext.Current.Handler;
}
public UrlParamsFilterHttpHandler(IHttpHandler originalHandler)
{
OriginalHandler = originalHandler;
}
public void ProcessRequest(HttpContext context)
{
//// do not worry, ProcessRequest() will not be called, but let's be safe
//throw new InvalidOperationException("MyHttpHandler cannot process requests.");
}
public bool IsReusable
{
// IsReusable must be set to false since class has a member!
get { return false; }
}
}
}asp.net windows phone sivlerlght
-
Saturday, February 25, 2012 12:51 AMusing System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.SessionState;
using TongCheng.Mobile.Framework.Business;
namespace TongCheng.Mobile.Wap
{
/// <summary>
///
/// </summary>
public class UrlAppendParams : System.Web.IHttpModule
{
private long refId = 0;
private string authCode = string.Empty;
public void Init(HttpApplication application)
{
application.AcquireRequestState += new EventHandler(Application_AcquireRequestState);
application.PostAcquireRequestState += new EventHandler(Application_PostAcquireRequestState);
application.PostMapRequestHandler += new EventHandler(Application_PostMapRequestHandler);
application.PostRequestHandlerExecute += new EventHandler(Application_ApplyUrlFilter);
}
#region Application_AcquireRequestState
private void Application_AcquireRequestState(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
this.refId = this.GetRefIdByRequest();
if (this.refId > 0)
{
UnionInfo unionInfo = new UnionInfo(this.refId);
unionInfo.AddToSession(SessionKey.UnionInfo);
}
this.authCode = this.GetAuthCodeByRequest();
if (!string.IsNullOrEmpty(this.authCode))
{
AuthenticationInfo authInfo = new AuthenticationInfo(this.authCode);
authInfo.AddToSession(SessionKey.AuthenticationInfo);
}
}
#endregion
#region Application_PostAcquireRequestState
private void Application_PostAcquireRequestState(object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
UrlParamsFilterHttpHandler resourceHttpHandler = HttpContext.Current.Handler as UrlParamsFilterHttpHandler;
if (resourceHttpHandler != null)
{
// set the original handler back
HttpContext.Current.Handler = resourceHttpHandler.OriginalHandler;
}
//// -> at this point session state should be available
//Debug.Assert(application.Session != null, "it did not work :(");
#region REFID
if (this.refId < 1)
{
this.refId = this.GetRefIdByRequest();
}
UnionInfo unionInfo = null;
if (this.refId > 0)
{
unionInfo = new UnionInfo(this.refId);
unionInfo.AddToSession(SessionKey.UnionInfo);
}
else
{
unionInfo = UnionInfo.GetCurrentUnionInfo();
}
if (HttpContext.Current != null && unionInfo != null)
{
if (!HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)))
HttpContext.Current.Items.Add(SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo), unionInfo);
else
HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)] = unionInfo;
}
#endregion
#region AUTHCODE
if (String.IsNullOrEmpty(this.authCode))
{
this.authCode = this.GetAuthCodeByRequest();
}
AuthenticationInfo authInfo = null;
if (!string.IsNullOrEmpty(this.authCode))
{
authInfo = new AuthenticationInfo(this.authCode);
authInfo.AddToSession(SessionKey.AuthenticationInfo);
}
else
{
authInfo = AuthenticationInfo.GetCurrentAuthenticationInfo();
}
if (HttpContext.Current != null && authInfo != null)
{
if (!HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)))
HttpContext.Current.Items.Add(SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo), authInfo);
else
HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)] = authInfo;
}
#endregion
}
#endregion
#region Application_PostMapRequestHandler
private void Application_PostMapRequestHandler(object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
if (application.Context.Handler is IReadOnlySessionState || application.Context.Handler is IRequiresSessionState)
{
// no need to replace the current handler
return;
}
// swap the current handler
application.Context.Handler = new UrlParamsFilterHttpHandler(application.Context.Handler);
}
#endregion
#region Application_ApplyUrlFilter
private void Application_ApplyUrlFilter(Object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
application.Response.Filter = new UrlAppendParamsFilter(application.Response.Filter);
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <returns></returns>
private long GetRefIdByRequest()
{
long refId = 0;
if (!string.IsNullOrEmpty(HttpContext.Current.Request["refid"])
&& Validator.IsInt64(HttpContext.Current.Request["refid"]))
{
refId = Int64.Parse(HttpContext.Current.Request["refid"]);
}
else if (!string.IsNullOrEmpty(HttpContext.Current.Request["unionId"])
&& Validator.IsInt64(HttpContext.Current.Request["unionId"]))
{
refId = Int64.Parse(HttpContext.Current.Request["unionId"]);
}
return refId;
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <returns></returns>
private string GetAuthCodeByRequest()
{
string auth = string.Empty;
if (!string.IsNullOrEmpty(HttpContext.Current.Request["auth"]))
{
auth = HttpContext.Current.Request["auth"];
}
return auth;
}
#endregion
public void Dispose()
{
}
#region CLASS UrlAppendParamsFilter
private class UrlAppendParamsFilter : Stream
{
private Stream Sink { get; set; }
private long _position;
private System.Text.StringBuilder oOutput = new StringBuilder();
public UrlAppendParamsFilter(Stream sink)
{
Sink = sink;
}
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override long Length
{
get { return 0; }
}
public override long Position
{
get { return _position; }
set { _position = value; }
}
public override long Seek(long offset, System.IO.SeekOrigin direction)
{
return Sink.Seek(offset, direction);
}
public override void SetLength(long length)
{
Sink.SetLength(length);
}
public override void Close()
{
Sink.Close();
}
public override void Flush()
{
Sink.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
return Sink.Read(buffer, offset, count);
}
public override void Write(byte[] buffer, int offset, int count)
{
string authCode = UrlAppendParamsFilter.GetAuthCodeByRequest();
long refId = UrlAppendParamsFilter.GetRefIdByRequest();
AuthenticationInfo authInfo = null;
UnionInfo unionInfo = null;
if (HttpContext.Current != null)
{
if (HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)))
authInfo = HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)] as AuthenticationInfo;
if (HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)))
unionInfo = HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)] as UnionInfo;
}
if ((refId <= 0 && unionInfo == null)
&& (string.IsNullOrEmpty(authCode) && authInfo == null))
{
Sink.Write(buffer, 0, buffer.Length);
return;
}
string content = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);
Regex regex = new Regex(RegexResource.HREF, RegexOptions.IgnoreCase);
Regex regexs = new Regex(RegexResource.HREFS, RegexOptions.IgnoreCase);
Regex action_regex = new Regex(RegexResource.ACTION, RegexOptions.IgnoreCase);
if (regex.IsMatch(content))
{
content = Regex.Replace(content, RegexResource.HREF, new MatchEvaluator(ReplaceParams), RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
if (regexs.IsMatch(content))
{
content = Regex.Replace(content, RegexResource.HREFS, new MatchEvaluator(ReplaceParams), RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
if (action_regex.IsMatch(content))
{
content = Regex.Replace(content, RegexResource.ACTION, new MatchEvaluator(ReplaceParams), RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(content);
Sink.Write(data, 0, data.Length);
}
public static string ReplaceParams(Match match)
{
string result = string.Empty;
if (match.Value.StartsWith("http", StringComparison.CurrentCultureIgnoreCase)
|| match.Value.StartsWith("wtai://", StringComparison.CurrentCultureIgnoreCase)
|| match.Value.EndsWith(".css", StringComparison.CurrentCultureIgnoreCase)
|| match.Value.EndsWith(".js", StringComparison.CurrentCultureIgnoreCase)
|| match.Value.EndsWith(".png", StringComparison.CurrentCultureIgnoreCase)
|| match.Value.EndsWith(".jpg", StringComparison.CurrentCultureIgnoreCase)
|| match.Value.EndsWith(".gif", StringComparison.CurrentCultureIgnoreCase))
{
result = match.Value;
}
else
{
#region
long refId = UrlAppendParamsFilter.GetRefIdByRequest();
if (refId <= 0)
{
UnionInfo unionInfo = null;
if (HttpContext.Current != null)
{
if (HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)))
unionInfo = HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)] as UnionInfo;
}
if (unionInfo != null)
{
refId = unionInfo.UnionId;
}
}
#endregion
#region
string authCode = UrlAppendParamsFilter.GetAuthCodeByRequest();
if (string.IsNullOrEmpty(authCode))
{
AuthenticationInfo authInfo = null;
if (HttpContext.Current != null)
{
if (HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)))
authInfo = HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)] as AuthenticationInfo;
}
if (authInfo != null)
{
authCode = authInfo.AuthCode;
}
}
#endregion
result = match.Value;
if (!string.IsNullOrEmpty(authCode) && result.IndexOf("auth=") == -1)
{
if (result.IndexOf('?') == -1)
{
result = result.Insert(result.Length, "?auth=" + authCode);
}
else
{
result = result.Insert(result.Length, "&auth=" + authCode);
}
}
if (refId > 0 && result.IndexOf("refid=") == -1)
{
if (result.IndexOf('?') == -1)
{
result = result.Insert(result.Length, "?refid=" + refId);
}
else
{
result = result.Insert(result.Length, "&refid=" + refId);
}
}
}
return result;
}
public class RegexResource
{
public static readonly string HREF = @"(?<=href=')(?<Link>.*?)(?=')";
public static readonly string HREFS = @"(?<=href=\"")(?<Link>.*?)(?=\"")";
//public static readonly string ACTION = "(<FORM.*ACTION=\")([^\"]*)(\"[^>]*>)";
public static readonly string ACTION = @"(?<=<\s*form\s.*?action\s*="").*?(?="".*?>)";
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private static string GetAuthCodeByRequest()
{
string auth = string.Empty;
if (!string.IsNullOrEmpty(HttpContext.Current.Request["auth"]))
{
auth = HttpContext.Current.Request["auth"];
}
return auth;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private static long GetRefIdByRequest()
{
long refId = 0;
if (!string.IsNullOrEmpty(HttpContext.Current.Request["refid"])
&& Validator.IsInt64(HttpContext.Current.Request["refid"]))
{
refId = Int64.Parse(HttpContext.Current.Request["refid"]);
}
else if (!string.IsNullOrEmpty(HttpContext.Current.Request["unionId"])
&& Validator.IsInt64(HttpContext.Current.Request["unionId"]))
{
refId = Int64.Parse(HttpContext.Current.Request["unionId"]);
}
return refId;
}
}
#endregion
}
// a temp handler used to force the SessionStateModule to load session state
public class UrlParamsFilterHttpHandler : IHttpHandler, IRequiresSessionState
{
internal readonly IHttpHandler OriginalHandler;
public UrlParamsFilterHttpHandler()
{
OriginalHandler = HttpContext.Current.Handler;
}
public UrlParamsFilterHttpHandler(IHttpHandler originalHandler)
{
OriginalHandler = originalHandler;
}
public void ProcessRequest(HttpContext context)
{
//// do not worry, ProcessRequest() will not be called, but let's be safe
//throw new InvalidOperationException("MyHttpHandler cannot process requests.");
}
public bool IsReusable
{
// IsReusable must be set to false since class has a member!
get { return false; }
}
}
}asp.net windows phone sivlerlght
-
Saturday, February 25, 2012 12:53 AMusing System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.SessionState;
using TongCheng.Mobile.Framework.Business;
namespace TongCheng.Mobile.Wap
{
/// <summary>
///
/// </summary>
public class UrlAppendParams : System.Web.IHttpModule
{
private long refId = 0;
private string authCode = string.Empty;
public void Init(HttpApplication application)
{
application.AcquireRequestState += new EventHandler(Application_AcquireRequestState);
application.PostAcquireRequestState += new EventHandler(Application_PostAcquireRequestState);
application.PostMapRequestHandler += new EventHandler(Application_PostMapRequestHandler);
application.PostRequestHandlerExecute += new EventHandler(Application_ApplyUrlFilter);
}
#region Application_AcquireRequestState
private void Application_AcquireRequestState(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
this.refId = this.GetRefIdByRequest();
if (this.refId > 0)
{
UnionInfo unionInfo = new UnionInfo(this.refId);
unionInfo.AddToSession(SessionKey.UnionInfo);
}
this.authCode = this.GetAuthCodeByRequest();
if (!string.IsNullOrEmpty(this.authCode))
{
AuthenticationInfo authInfo = new AuthenticationInfo(this.authCode);
authInfo.AddToSession(SessionKey.AuthenticationInfo);
}
}
#endregion
#region Application_PostAcquireRequestState
private void Application_PostAcquireRequestState(object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
UrlParamsFilterHttpHandler resourceHttpHandler = HttpContext.Current.Handler as UrlParamsFilterHttpHandler;
if (resourceHttpHandler != null)
{
// set the original handler back
HttpContext.Current.Handler = resourceHttpHandler.OriginalHandler;
}
//// -> at this point session state should be available
//Debug.Assert(application.Session != null, "it did not work :(");
#region REFID
if (this.refId < 1)
{
this.refId = this.GetRefIdByRequest();
}
UnionInfo unionInfo = null;
if (this.refId > 0)
{
unionInfo = new UnionInfo(this.refId);
unionInfo.AddToSession(SessionKey.UnionInfo);
}
else
{
unionInfo = UnionInfo.GetCurrentUnionInfo();
}
if (HttpContext.Current != null && unionInfo != null)
{
if (!HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)))
HttpContext.Current.Items.Add(SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo), unionInfo);
else
HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)] = unionInfo;
}
#endregion
#region AUTHCODE
if (String.IsNullOrEmpty(this.authCode))
{
this.authCode = this.GetAuthCodeByRequest();
}
AuthenticationInfo authInfo = null;
if (!string.IsNullOrEmpty(this.authCode))
{
authInfo = new AuthenticationInfo(this.authCode);
authInfo.AddToSession(SessionKey.AuthenticationInfo);
}
else
{
authInfo = AuthenticationInfo.GetCurrentAuthenticationInfo();
}
if (HttpContext.Current != null && authInfo != null)
{
if (!HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)))
HttpContext.Current.Items.Add(SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo), authInfo);
else
HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)] = authInfo;
}
#endregion
}
#endregion
#region Application_PostMapRequestHandler
private void Application_PostMapRequestHandler(object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
if (application.Context.Handler is IReadOnlySessionState || application.Context.Handler is IRequiresSessionState)
{
// no need to replace the current handler
return;
}
// swap the current handler
application.Context.Handler = new UrlParamsFilterHttpHandler(application.Context.Handler);
}
#endregion
#region Application_ApplyUrlFilter
private void Application_ApplyUrlFilter(Object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
application.Response.Filter = new UrlAppendParamsFilter(application.Response.Filter);
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <returns></returns>
private long GetRefIdByRequest()
{
long refId = 0;
if (!string.IsNullOrEmpty(HttpContext.Current.Request["refid"])
&& Validator.IsInt64(HttpContext.Current.Request["refid"]))
{
refId = Int64.Parse(HttpContext.Current.Request["refid"]);
}
else if (!string.IsNullOrEmpty(HttpContext.Current.Request["unionId"])
&& Validator.IsInt64(HttpContext.Current.Request["unionId"]))
{
refId = Int64.Parse(HttpContext.Current.Request["unionId"]);
}
return refId;
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <returns></returns>
private string GetAuthCodeByRequest()
{
string auth = string.Empty;
if (!string.IsNullOrEmpty(HttpContext.Current.Request["auth"]))
{
auth = HttpContext.Current.Request["auth"];
}
return auth;
}
#endregion
public void Dispose()
{
}
#region CLASS UrlAppendParamsFilter
private class UrlAppendParamsFilter : Stream
{
private Stream Sink { get; set; }
private long _position;
private System.Text.StringBuilder oOutput = new StringBuilder();
public UrlAppendParamsFilter(Stream sink)
{
Sink = sink;
}
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override long Length
{
get { return 0; }
}
public override long Position
{
get { return _position; }
set { _position = value; }
}
public override long Seek(long offset, System.IO.SeekOrigin direction)
{
return Sink.Seek(offset, direction);
}
public override void SetLength(long length)
{
Sink.SetLength(length);
}
public override void Close()
{
Sink.Close();
}
public override void Flush()
{
Sink.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
return Sink.Read(buffer, offset, count);
}
public override void Write(byte[] buffer, int offset, int count)
{
string authCode = UrlAppendParamsFilter.GetAuthCodeByRequest();
long refId = UrlAppendParamsFilter.GetRefIdByRequest();
AuthenticationInfo authInfo = null;
UnionInfo unionInfo = null;
if (HttpContext.Current != null)
{
if (HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)))
authInfo = HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)] as AuthenticationInfo;
if (HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)))
unionInfo = HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)] as UnionInfo;
}
if ((refId <= 0 && unionInfo == null)
&& (string.IsNullOrEmpty(authCode) && authInfo == null))
{
Sink.Write(buffer, 0, buffer.Length);
return;
}
string content = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);
Regex regex = new Regex(RegexResource.HREF, RegexOptions.IgnoreCase);
Regex regexs = new Regex(RegexResource.HREFS, RegexOptions.IgnoreCase);
Regex action_regex = new Regex(RegexResource.ACTION, RegexOptions.IgnoreCase);
if (regex.IsMatch(content))
{
content = Regex.Replace(content, RegexResource.HREF, new MatchEvaluator(ReplaceParams), RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
if (regexs.IsMatch(content))
{
content = Regex.Replace(content, RegexResource.HREFS, new MatchEvaluator(ReplaceParams), RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
if (action_regex.IsMatch(content))
{
content = Regex.Replace(content, RegexResource.ACTION, new MatchEvaluator(ReplaceParams), RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(content);
Sink.Write(data, 0, data.Length);
}
public static string ReplaceParams(Match match)
{
string result = string.Empty;
if (match.Value.StartsWith("http", StringComparison.CurrentCultureIgnoreCase)
|| match.Value.StartsWith("wtai://", StringComparison.CurrentCultureIgnoreCase)
|| match.Value.EndsWith(".css", StringComparison.CurrentCultureIgnoreCase)
|| match.Value.EndsWith(".js", StringComparison.CurrentCultureIgnoreCase)
|| match.Value.EndsWith(".png", StringComparison.CurrentCultureIgnoreCase)
|| match.Value.EndsWith(".jpg", StringComparison.CurrentCultureIgnoreCase)
|| match.Value.EndsWith(".gif", StringComparison.CurrentCultureIgnoreCase))
{
result = match.Value;
}
else
{
#region
long refId = UrlAppendParamsFilter.GetRefIdByRequest();
if (refId <= 0)
{
UnionInfo unionInfo = null;
if (HttpContext.Current != null)
{
if (HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)))
unionInfo = HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.UnionInfo)] as UnionInfo;
}
if (unionInfo != null)
{
refId = unionInfo.UnionId;
}
}
#endregion
#region
string authCode = UrlAppendParamsFilter.GetAuthCodeByRequest();
if (string.IsNullOrEmpty(authCode))
{
AuthenticationInfo authInfo = null;
if (HttpContext.Current != null)
{
if (HttpContext.Current.Items.Contains(SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)))
authInfo = HttpContext.Current.Items[SessionKeyHelper.GetSessionKey(SessionKey.AuthenticationInfo)] as AuthenticationInfo;
}
if (authInfo != null)
{
authCode = authInfo.AuthCode;
}
}
#endregion
result = match.Value;
if (!string.IsNullOrEmpty(authCode) && result.IndexOf("auth=") == -1)
{
if (result.IndexOf('?') == -1)
{
result = result.Insert(result.Length, "?auth=" + authCode);
}
else
{
result = result.Insert(result.Length, "&auth=" + authCode);
}
}
if (refId > 0 && result.IndexOf("refid=") == -1)
{
if (result.IndexOf('?') == -1)
{
result = result.Insert(result.Length, "?refid=" + refId);
}
else
{
result = result.Insert(result.Length, "&refid=" + refId);
}
}
}
return result;
}
public class RegexResource
{
public static readonly string HREF = @"(?<=href=')(?<Link>.*?)(?=')";
public static readonly string HREFS = @"(?<=href=\"")(?<Link>.*?)(?=\"")";
//public static readonly string ACTION = "(<FORM.*ACTION=\")([^\"]*)(\"[^>]*>)";
public static readonly string ACTION = @"(?<=<\s*form\s.*?action\s*="").*?(?="".*?>)";
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private static string GetAuthCodeByRequest()
{
string auth = string.Empty;
if (!string.IsNullOrEmpty(HttpContext.Current.Request["auth"]))
{
auth = HttpContext.Current.Request["auth"];
}
return auth;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private static long GetRefIdByRequest()
{
long refId = 0;
if (!string.IsNullOrEmpty(HttpContext.Current.Request["refid"])
&& Validator.IsInt64(HttpContext.Current.Request["refid"]))
{
refId = Int64.Parse(HttpContext.Current.Request["refid"]);
}
else if (!string.IsNullOrEmpty(HttpContext.Current.Request["unionId"])
&& Validator.IsInt64(HttpContext.Current.Request["unionId"]))
{
refId = Int64.Parse(HttpContext.Current.Request["unionId"]);
}
return refId;
}
}
#endregion
}
// a temp handler used to force the SessionStateModule to load session state
public class UrlParamsFilterHttpHandler : IHttpHandler, IRequiresSessionState
{
internal readonly IHttpHandler OriginalHandler;
public UrlParamsFilterHttpHandler()
{
OriginalHandler = HttpContext.Current.Handler;
}
public UrlParamsFilterHttpHandler(IHttpHandler originalHandler)
{
OriginalHandler = originalHandler;
}
public void ProcessRequest(HttpContext context)
{
//// do not worry, ProcessRequest() will not be called, but let's be safe
//throw new InvalidOperationException("MyHttpHandler cannot process requests.");
}
public bool IsReusable
{
// IsReusable must be set to false since class has a member!
get { return false; }
}
}
}asp.net windows phone sivlerlght
-
Saturday, February 25, 2012 2:54 AM
You're best off profiling the code if you are having performance issues and determining where the bottlenecks are. There is way, way too much code there for us to sort through it all line by line. If you can come up with a few sections of code that are less than a dozen lines or so I'm sure someone will be able to point out ways that they might be improved.
-
Monday, February 27, 2012 3:11 AM
Thank you for you reply.
I have trace and monitor the project and find the problem section in the the event
Application_PostMapRequestHandler the detail as below: I don't know the reason case problem,thank you very much.
private void Application_PostMapRequestHandler(object source, EventArgs e)
// a temp handler used to force the SessionStateModule to load session state
{
HttpApplication application = (HttpApplication)source;
if (application.Context.Handler is IReadOnlySessionState || application.Context.Handler is IRequiresSessionState)
{
// no need to replace the current handler
return;
}
// swap the current handler
application.Context.Handler = new UrlParamsFilterHttpHandler(application.Context.Handler);
}
public class UrlParamsFilterHttpHandler : IHttpHandler, IRequiresSessionState
{
internal readonly IHttpHandler OriginalHandler;
public UrlParamsFilterHttpHandler()
{
OriginalHandler = HttpContext.Current.Handler;
HttpContext.Current.Trace.Write(MethodInfo.GetCurrentMethod().Name, DateTime.Now.ToLongTimeString());
}
public UrlParamsFilterHttpHandler(IHttpHandler originalHandler)
{
OriginalHandler = originalHandler;
HttpContext.Current.Trace.Write(MethodInfo.GetCurrentMethod().Name, DateTime.Now.ToLongTimeString());
}
public void ProcessRequest(HttpContext context)
{
HttpContext.Current.Trace.Write(MethodInfo.GetCurrentMethod().Name, DateTime.Now.ToLongTimeString());
//// do not worry, ProcessRequest() will not be called, but let's be safe
//throw new InvalidOperationException("MyHttpHandler cannot process requests.");
}
public bool IsReusable
{
// IsReusable must be set to false since class has a member!
get { return false; }
}
}
asp.net windows phone sivlerlght
-
Tuesday, February 28, 2012 5:54 AM
I would suggest you to post it at ASP.Net forums for better support, where experts live in.
Hope it helps.
Best Regards,
Rocky Yue[MSFT]
MSDN Community Support | Feedback to us

