📄 customredirectinfo.cs
字号:
namespace ASPNET.StarterKit.Communities
{
using System;
/// <summary>
/// Instance object for Custom Redirection mod. Contains info about a specific redirection.
/// </summary>
public class CustomRedirectInfo : IComparable
{
string _sourceURL = String.Empty;
string _destinationURL = String.Empty;
bool _isSectionRedirect;
bool _isValidSource = true;
bool _isValidDestination = true;
string _sourceValidationErrorMessage = String.Empty;
string _destinationValidationErrorMessage = String.Empty;
bool _destinationIsPath;
/// <summary>
/// true if Destination URL is a path. false if it is a specific file.
/// </summary>
public bool DestinationIsPath
{
get{return _destinationIsPath;}
set{_destinationIsPath = value;}
}
/// <summary>
/// true if both source and destination URLs are valid. False otherwise
/// </summary>
public bool IsValidRedirect
{
get
{
return (_isValidSource && _isValidDestination);
}
}
/// <summary>
/// true if SourceUrl is valid.
/// </summary>
public bool IsValidSource
{
get{return _isValidSource;}
set{_isValidSource = value;}
}
/// <summary>
/// true if DestinationUrl is valid.
/// </summary>
public bool IsValidDestination
{
get{return _isValidDestination;}
set{_isValidDestination = value;}
}
/// <summary>
/// A friendly error indicating why the source URL is not valid.
/// </summary>
public string SourceValidationErrorMessage
{
get{return _sourceValidationErrorMessage;}
set{_sourceValidationErrorMessage = value;}
}
/// <summary>
/// A friendly error indicating why the destination URL is not valid.
/// </summary>
public string DestinationValidationErrorMessage
{
get{return _destinationValidationErrorMessage;}
set{_destinationValidationErrorMessage = value;}
}
/// <summary>
/// The URL that should be redirected
/// </summary>
public string SourceURL
{
get{return _sourceURL;}
set{_sourceURL = value;}
}
/// <summary>
/// The URL to redirect the browser to
/// </summary>
public string DestinationURL
{
get{return _destinationURL;}
set{_destinationURL = value;}
}
/// <summary>
/// Indicates if the redirection is for a specific page or a whole section
/// </summary>
public bool IsSectionRedirect
{
get{return _isSectionRedirect;}
set{_isSectionRedirect = value;}
}
/// <summary>
/// Constructor. Populates Redirect Info from DataReader.
/// </summary>
/// <remarks>
/// This does validate the URLs, but since URLs come from the database
/// they should already be valid.
/// </remarks>
/// <param name="dr">DataReader containing valid redirect row from DB.</param>
public CustomRedirectInfo(System.Data.SqlClient.SqlDataReader dr)
{
this._sourceURL = ((string)dr["redirect_SourceURL"]);
this._destinationURL = ((string)dr["redirect_DestinationURL"]);
this._isSectionRedirect = (bool)dr["redirect_isSectionRedirect"];
this._destinationIsPath = (CommunityGlobals.GetPageName("/" + this._destinationURL) == "/");
this.Validate();
}
/// <summary>
/// Constructor. Populates Redirect Info and validates URLs.
/// </summary>
/// <param name="sourceUrl">Source Url to redirect from.</param>
/// <param name="destinationUrl">Destination URL to redirect to.</param>
/// <param name="sourceIsPath">true if source is a section path.</param>
/// <param name="destinationIsPath">true is destination is a path.</param>
public CustomRedirectInfo(string sourceUrl,string destinationUrl, bool sourceIsPath, bool destinationIsPath)
{
this._sourceURL = sourceUrl.Trim();
this._destinationURL = destinationUrl.Trim();
this._isSectionRedirect = sourceIsPath;
this._destinationIsPath = destinationIsPath;
this.Validate();
}
private void Validate()
{
// Validate source URL
if(this._sourceURL.ToLower().StartsWith("http://"))
{
this._isValidSource = false;
this._sourceValidationErrorMessage = "Source URL must be relative to application root.";
}
else
{
if(this._sourceURL.ToLower().StartsWith("/"))
{
this._sourceURL = this._sourceURL.Substring(1,this._sourceURL.Length -1);
}
if(this._isSectionRedirect)
{
if(CommunityGlobals.GetPageName("/" + this._sourceURL) != "/")
{
this._isValidSource = false;
this._sourceValidationErrorMessage = "A path URL must end with a \"/\" character.";
}
}
else
{
if(CommunityGlobals.GetPageName("/" + this._sourceURL) == "/")
{
this._isValidSource = false;
this._sourceValidationErrorMessage = "Exact Page URL must include the page or file name and cannot end with a \"/\" character.";
}
}
}
// Validate destination URL
if(!this._destinationURL.ToLower().StartsWith("http://"))
{
if(this._destinationURL.StartsWith("/"))
{
this._destinationURL = this._destinationURL.Substring(1,this._destinationURL.Length -1);
}
if(this._destinationIsPath)
{
if(CommunityGlobals.GetPageName("/" + this._destinationURL) != "/")
{
this._isValidDestination = false;
this._destinationValidationErrorMessage = "A Path URL must end with a \"/\" character.";
}
}
else
{
if(CommunityGlobals.GetPageName("/" + this._destinationURL) == "/")
{
this._isValidDestination = false;
this._destinationValidationErrorMessage = "Exact Page URL must include the page or file name and cannot end with a \"/\" character.";
}
}
}
}
/// <summary>
/// Necessary for sorting in arrays. Implementation for IComparable interface.
/// </summary>
/// <param name="obj">An object that will cast to a CustomRedirectInfo object</param>
/// <returns>Int: See Boolean.CompareTo in .NET Framework documentation.</returns>
public int CompareTo(object obj)
{
if(obj is CustomRedirectInfo)
{
CustomRedirectInfo ri = (CustomRedirectInfo)obj;
return _isSectionRedirect.CompareTo(ri.IsSectionRedirect);
}
throw new ArgumentException("object is not a CustomRedirectInfo");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -