5

I have a project on .Net Framework 2.0 in which i need to call some pages without page extension, that means I have to remove the .aspx from url and also I need to pass some Query String data. URL rewrite has been implemented currently in the following way 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 in order to achieve the url rewrite without .aspx extension, by passing some query string parameters in .Net framework 2.0

Animesh Anand
  • 314
  • 4
  • 13
  • It is not clear which URLs you need to redirect to the ones without .aspx. Could you be more specific? I expect that those are not all URLs because you explicitly add ".aspx" in quite a few `destination`s. – SergGr Nov 03 '17 at 14:46
  • Did you try the solution suggested in this post: https://stackoverflow.com/questions/4481632/remove-html-or-aspx-extension. This should be your first choice since you are on .Net 2.0. – Shashank Chaturvedi Nov 09 '17 at 18:23
  • "by passing some query string parameters" Are you saying you would like to append some query string parameters to the Request for redirection? (Once Request has been received?) – DaniDev Nov 09 '17 at 22:51

3 Answers3

3

When you address a .aspx page, you address it directly. Meaning, that you have to address the url as .aspx, else it won't find the requested page, because there aren't any. This is the big advantage MVC brought, when you can address your cshtml view and the url will show /Home/Index without the cshtml extenstion.

There are two ways to achieve what you ask, when it comes to .aspx page.

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);
    } 
}  

you override the map page route and declare the routes without .aspx extension, the way MVC does. This is called custom route handler using Url Routing

Url Rewrite

As you did, url rewrite through web.config.

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

You use the url mapping to map you .aspx url to a new route, this way you allow your web forms pages to rewrite the url without the .aspx extension.

<rule source> 

Won't work as it does not rewrite your url, it is directing you to the exact file so if you do not use the .aspx extension, the file does not exist and exception is thrown. You will have to use UrlMappings if you want to remove the .aspx extension.

Barr J
  • 10,636
  • 1
  • 28
  • 46
0

As I wrote in the comment it is not clear which URLs should be redirected without ".aspx" but still, why can't you do the same trick you already do for ".php" such as in

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

So assuming you want to change URLs under old "Admin/TheFetus" folder, you might change your first rule to something like:

    <rule source="Admin/TheFetus/(.*).aspx(.*)" destination="Admin/Fetus/$1$2"/>
SergGr
  • 23,570
  • 2
  • 30
  • 51
  • In this line, the new url will have .aspx extension, which I don't want. – Animesh Anand Nov 06 '17 at 07:34
  • I tried this But I got 404 - File or directory not found error when I called Client/CME/CMEPortal.aspx page. – Animesh Anand Nov 06 '17 at 07:59
  • @AnimeshAnand, the obvious question is: does it work if you open `Client/CME/CMEPortal` (i.e. without .aspx) directly? You might have a different problem with routing here. – SergGr Nov 06 '17 at 15:32
  • No, it does not work with Client/CME/CMEPortal , I'm using .Net framework 2.0, what might be causing issue? I've no idea – Animesh Anand Nov 07 '17 at 12:08
0

I think what will work well for you is a simple Custom Filter Module: You can Build this as a DLL Project and Target .net 2.0 - 3.5.

This will enable you rewrite and redirect the request using all String functions as well ass all HTTP classes (Request, Response Etc. )

(Since I don't understand your redirect rules specifically, I can't do that part for you, but I am sure you can figure it out). Otherwise please explain the rules better and I will be happy to do so.

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()
        {

        }

    }
}

You can then implement the module just by adding/ editing your Web.Config, accordingly:

Implementation

1.Add the compiled DLL (FilterModule.dll) to your site's bin Directory.

2.Add the following to module definition in the Web Service (or site) configuration file (web.config)

in the <system.webServer> section under <modules>.

add the following:

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

Update: I have since tested my answer and it works as expected. Please let me know if you have any trouble implementing it.

DaniDev
  • 2,471
  • 2
  • 22
  • 29