[英]URL Rewriting exceptional case for removing .aspx extension
我的情况是:我有一个ASP.NET WebForm网站。 用户可以在我的网站上创建自己的页面,其页面URL类似于以下内容:(MyWebsite.com/UserPage)。 但实际上是:(MyWebsite.com/UserPages.aspx?q=UserPage)。 这意味着当您输入URL(MyWebsite.com/UserPage)时,它将重写URL并显示(MyWebsite.com/UserPages.aspx?q=UserPage)(但地址栏始终类似于(MyWebsite.com/UserPage)。这是我的“ UrlRewriting”类中的代码:
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
if (app.Request.Path.Contains("/") && !app.Request.Path.Contains(".") && app.Request.Path.IndexOf("/") == app.Request.Path.LastIndexOf("/"))
{
string userPageTitle = app.Request.Path.Substring(app.Request.Path.IndexOf("/") + 1);
if (!string.IsNullOrEmpty(userPageTitle ))
{
app.Context.RewritePath(string.Format("UserPages.aspx?q={0}", userPageTitle));
}
}
}
现在这是我的问题:正如我说我的项目是ASP.NET WebForm,(因此,所有页面都具有.aspx扩展名),我想删除Urls中的.aspx扩展名,我已经尝试了web.config中的一些代码,正常工作(在正常情况下),但对于我来说,如果输入(MyWebsite.com/UserPage),它将把该“ UserPage”视为“ UserPage.aspx”。 我该如何处理?
我通常使用ASP.NET Web Forms 4+中可用的路由来做到这一点。
您在Global.asax
注册路由(URL模式),并指定哪个ASPX页面将处理该URL。
此示例将让UserPage.aspx处理所有其他ASPX页面无法处理的URL。
void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("UserPageRoute", "{*url}", "~/UserPage.aspx");
}
然后,在UserPage.aspx
,可以通过查看Request.Url
对象来确定请求的URL,例如。 Request.Url.PathAndQuery
。
请注意,您可能需要一些额外的web.config
设置才能起作用,例如(以管理无扩展名的URL请求)...
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.