隐藏页面扩展时使用查询字符串的问题
Problems using query strings when hiding page extension
我正在使用 ASP.NET 和 C# (Visual Studio 2015)。我在 Global.asax
文件中实现了以下代码片段:
void Application_BeginRequest(object sender, EventArgs e)
{
//Removes requirement of having ".aspx" in the URL
String WebsiteURL = Request.Url.ToString();
String[] SplitedURL = WebsiteURL.Split('/');
String[] Temp = SplitedURL[SplitedURL.Length - 1].Split('.');
// This is for aspx page
if (!WebsiteURL.Contains(".aspx") && Temp.Length == 1)
{
if (!string.IsNullOrEmpty(Temp[0].Trim()))
Context.RewritePath(Temp[0] + ".aspx");
}
}
这样就不会在页面中强制扩展 URL:
我可以省略扩展名以重定向到同一页面:
问题是,现在如果我将查询字符串添加到 URL,它就无法再找到该页面。例如:
导致 404 未找到错误。
如何使查询字符串在隐藏扩展时也能正常工作?
void Application_BeginRequest(object sender, EventArgs e)
{
//Removes requirement of having ".aspx" in the URL
Uri uri = this.Request.Url;
string path = uri.AbsolutePath;
if (!string.IsNullOrEmpty(path) && path != "/" && path.IndexOf('.') == -1)
{
path = path + ".aspx";
string query = uri.Query;
string url = path + query;
Context.RewritePath(url);
}
}
试试上面的代码。正如 VDWWD 提到的,最好的方法是使用 IIS url 重写器。
我正在使用 ASP.NET 和 C# (Visual Studio 2015)。我在 Global.asax
文件中实现了以下代码片段:
void Application_BeginRequest(object sender, EventArgs e)
{
//Removes requirement of having ".aspx" in the URL
String WebsiteURL = Request.Url.ToString();
String[] SplitedURL = WebsiteURL.Split('/');
String[] Temp = SplitedURL[SplitedURL.Length - 1].Split('.');
// This is for aspx page
if (!WebsiteURL.Contains(".aspx") && Temp.Length == 1)
{
if (!string.IsNullOrEmpty(Temp[0].Trim()))
Context.RewritePath(Temp[0] + ".aspx");
}
}
这样就不会在页面中强制扩展 URL:
我可以省略扩展名以重定向到同一页面:
问题是,现在如果我将查询字符串添加到 URL,它就无法再找到该页面。例如:
导致 404 未找到错误。
如何使查询字符串在隐藏扩展时也能正常工作?
void Application_BeginRequest(object sender, EventArgs e)
{
//Removes requirement of having ".aspx" in the URL
Uri uri = this.Request.Url;
string path = uri.AbsolutePath;
if (!string.IsNullOrEmpty(path) && path != "/" && path.IndexOf('.') == -1)
{
path = path + ".aspx";
string query = uri.Query;
string url = path + query;
Context.RewritePath(url);
}
}
试试上面的代码。正如 VDWWD 提到的,最好的方法是使用 IIS url 重写器。