【问题标题】:URL Rewriting exceptional case for removing .aspx extension删除 .aspx 扩展名的 URL 重写例外情况
【发布时间】:2018-07-28 18:01:47
【问题描述】:

我的场景是:我有一个 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 url-rewriting webforms asp.net-webpages


    【解决方案1】:

    我通常使用 ASP.NET Web Forms 4+ 中提供的 Routing 来执行此操作。

    您在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" />
    

    【讨论】:

      猜你喜欢
      • 2013-09-15
      • 2022-03-04
      • 2019-04-21
      • 2014-04-15
      • 2014-01-25
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多