简体   繁体   English

重写.aspx扩展名的URL重写例外情况

[英]URL Rewriting exceptional case for removing .aspx extension

My scenario is: I have a website which is ASP.NET WebForm. 我的情况是:我有一个ASP.NET WebForm网站。 Users can create their own page on my web site, their page url would be something like this: (MyWebsite.com/UserPage). 用户可以在我的网站上创建自己的页面,其页面URL类似于以下内容:(MyWebsite.com/UserPage)。 but It is actually: (MyWebsite.com/UserPages.aspx?q=UserPage). 但实际上是:(MyWebsite.com/UserPages.aspx?q=UserPage)。 It means when you enter the url (MyWebsite.com/UserPage) It rewrites the url and shows you (MyWebsite.com/UserPages.aspx?q=UserPage) (but the address bar is always like (MyWebsite.com/UserPage). Here's my code in my "UrlRewriting" class: 这意味着当您输入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));
            }
        } 
   }

Now here's my problem: as I said my project is ASP.NET WebForm, (So, all of pages have .aspx extension) I wanted to remove the .aspx extension in my Urls, I've tried some codes in web.config which were working properly (In normal cases), but In my case, if you enter (MyWebsite.com/UserPage) It will be considering this "UserPage", as "UserPage.aspx". 现在这是我的问题:正如我说我的项目是ASP.NET WebForm,(因此,所有页面都具有.aspx扩展名),我想删除Urls中的.aspx扩展名,我已经尝试了web.config中的一些代码,正常工作(在正常情况下),但对于我来说,如果输入(MyWebsite.com/UserPage),它将把该“ UserPage”视为“ UserPage.aspx”。 How can I handle this? 我该如何处理?

I usually do this with Routing which is available in ASP.NET Web Forms 4+. 我通常使用ASP.NET Web Forms 4+中可用的路由来做到这一点。

You register your routes (URL patterns) in Global.asax , and specify which ASPX page will handle that URL. 您在Global.asax注册路由(URL模式),并指定哪个ASPX页面将处理该URL。

This example would have UserPage.aspx handle all URLs that weren't otherwise handled by other ASPX pages. 此示例将让UserPage.aspx处理所有其他ASPX页面无法处理的URL。

void Application_Start(object sender, EventArgs e)
{
    RouteTable.Routes.MapPageRoute("UserPageRoute", "{*url}", "~/UserPage.aspx");
}

Then in your UserPage.aspx you can determine the URL requested by looking at the Request.Url object, eg. 然后,在UserPage.aspx ,可以通过查看Request.Url对象来确定请求的URL,例如。 Request.Url.PathAndQuery . Request.Url.PathAndQuery

Note that you may need some extra web.config settings for this to work, eg (to manage extensionless URL requests)... 请注意,您可能需要一些额外的web.config设置才能起作用,例如(以管理无扩展名的URL请求)...

<configuration>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM