Rewrite URL Rule Without Page Extension

I have a .Net Framework 2.0 project in which I need to call some pages without a page extension, this means that I need to remove .aspx from the URL, and I also need to pass some query String data. URL renaming has now been implemented as follows, but it does not remove .aspx

<configuration>
<modulesSection>
    <rewriteModule>
      <rewriteOn>true</rewriteOn>
      <rewriteRules>
        <rule source="Admin/TheFetus/(.*)" destination="Admin/Fetus/$1"/>
        <rule source="CaseDetails/(.*).aspx" destination="Client/Cases/CaseDetails.aspx"/>
        <!--<rule source="ArticleDetails/(.*).aspx" destination="Client/Articles/ArticleDetails.aspx"/>-->
        <rule source="ArticleDetails" destination="Client/Articles/ArticleDetails.aspx"/>
        <rule source="ChapterDetails/(.*).aspx" destination="Client/Chapters/ChapterDetails.aspx"/>
        <rule source="LectureDetails/(.*).aspx" destination="Client/Lectures/LectureDetails.aspx"/>
        <rule source="ConventionDetails/(.*).aspx" destination="Client/Conventions/ConventionDetails.aspx"/>
        <rule source="IfserDetails/(.*).aspx" destination="Client/Ifser/IfserDetails.aspx"/>
        <rule source="Client/Fetus/Files/(.*)" destination="Client/Fetus/Files/$1"/>
        <rule source="Fetus/Files/(.*)" destination="Client/Fetus/Files/$1"/>
        <rule source="Client/Fetus/Index.php" destination="Client/Fetus/Home.aspx"/>
        <rule source="Fetus/Index.php" destination="Client/Fetus/Home.aspx"/>
        <rule source="Client/Fetus/(.*).php(.*)" destination="Client/Fetus/$1.aspx$2"/>
        <rule source="Fetus/(.*).php(.*)" destination="Client/Fetus/$1.aspx$2"/>
        <rule source="Admin/Fetus/(.*)" destination="Admin/Fetus/$1"/>
        <rule source="Client/Fetus/(.*)" destination="Client/Fetus/$1"/>
        <rule source="Fetus/(.*)" destination="Client/Fetus/$1"/>
        <rule source="bannerspecs" destinatiofn="Client/FooterLinks/BannerSpecs.aspx"/>
        <rule source="Client/TheFetus/Files/(.*)" destination="Client/Fetus/Files/$1"/>
        <rule source="TheFetus/Files/(.*)" destination="Client/Fetus/Files/$1"/>
        <rule source="Client/TheFetus/Index.php" destination="Client/Fetus/Home.aspx"/>
        <rule source="TheFetus/Index.php" destination="Client/Fetus/Home.aspx"/>
        <rule source="Client/TheFetus/(.*).php(.*)" destination="Client/Fetus/$1.aspx$2"/>
        <rule source="TheFetus/(.*).php(.*)" destination="Client/Fetus/$1.aspx$2"/>
        <rule source="Client/TheFetus/(.*).php(.*)" destination="Client/Fetus/$1.aspx$2"/>
        <rule source="Client/TheFetus/(.*)" destination="Client/Fetus/$1"/>
        <rule source="TheFetus/(.*)" destination="Client/Fetus/$1"/>
        <rule source="(.*)/Default.aspx" destination="Default.aspx?Folder=$1"/>
        <rule source="LATAM.aspx" destination="Client/MiniSites/MiniSiteDetails.aspx?MiniSiteId=10"/>
      </rewriteRules>
    </rewriteModule>
  </modulesSection>
</configuration>

How can I replace the current web.config code so that I can rewrite the url without the .aspx extension by passing some query string parameters to the .Net framework 2.0

+4
source share
3 answers

.aspx, . , url .aspx, , . MVC, cshtml, url // cshtml.

, , .aspx.

Globl.asax

public class Global : System.Web.HttpApplication
{

    //Register your routes, match a custom URL with an .aspx file. 
    private void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("About", "about", "~/about.aspx");
        routes.MapPageRoute("Index", "index", "~/index.aspx");
    }

    //Init your new route table inside the App_Start event.
    protected void Application_Start(object sender, EventArgs e)
    {
        this.RegisterRoutes(RouteTable.Routes);
    } 
}  

.aspx, MVC. , Url Routing

Url Rewrite

, url web.config.

<urlMappings enabled="true">
 <add url="~/questions/ask" mappedUrl="~/questions/ask.aspx?page=Ask"/>
</urlMappings>

url URL- .aspx , - URL- .aspx.

<rule source> 

, URL-, , , .aspx, . UrlMappings, .aspx.

+2

, , URL- ".aspx", , , ".php", ,

    <rule source="Client/Fetus/(.*).php(.*)" destination="Client/Fetus/$1.aspx$2"/>

, URL- "Admin/TheFetus", - :

    <rule source="Admin/TheFetus/(.*).aspx(.*)" destination="Admin/Fetus/$1$2"/>
0

, , : DLL Target.net 2.0 - 3.5.

, String, HTTP (Request, Response Etc.)

( , , , ). , , , .

using System;
using System.Web;
using System.Threading;
//using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FilterModule
{
    class AspxFilter : IHttpModule
    {
        public void Init(HttpApplication app)
        {
            app.BeginRequest += new EventHandler(AspxRedirect);
        }



        private void AspxRedirect(Object s, EventArgs e)
        {
            HttpApplication app = s as HttpApplication;
            HttpRequest req = app.Request;
            HttpContext context = app.Context;

            string filePath = context.Request.FilePath;
            string fileExtension = VirtualPathUtility.GetExtension(filePath);
            string fileName = VirtualPathUtility.GetFileName(filePath);

            if (fileExtension.ToLower() == ".aspx")
            {

               if (req.QueryString["Redirect"] != null)
               {
                String RedirectPath = "Redirect.html";
                // Build your redirect Path as needed based on the rquest String
                context.Response.Redirect(RedirectPath);
                }
                else
                {
                }

            }

        }

        public void Dispose()
        {

        }

    }
}

, / Web.Config, :

1. DLL (FilterModule.dll) bin.

2. - ( ) (web.config)

<system.webServer> <modules>.

:

<system.webServer>
    <modules>
    <add name ="FilterModule" type="FilterModule.AspxFilter" />

Update: Since then I have checked my answer and it works as expected. Please let me know if you have problems implementing it.

0
source

Source: https://habr.com/ru/post/1688521/


All Articles