Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

ASP Core: Redirect Rewrite von URL Adressen

26.07.2021 (๐Ÿ‘17979)


Wie kann man URL Pfade in ASP Core .Net5 auf einen anderen Pfad umlenken?

Aus einer alten Anwendung sollen die URL Adressen umgeleitet werden auf neue Pfade in der Asp Core Anwendung.

Dabei sollen die in Google und anderen SEO Suchmaschinen gespeicherten URL nicht verloren gehen.



Vorgehensweise:

Bei Eingabe in Browser

Wenn eine alte URL eingegeben wird, dann soll diese umgeleitet werden in eine Neue Adresse

Nach Umleitung

Nach der Eingabe des Links wird der Pfad in eine neue ASP MVC Adresse nach neuen Vorgaben umgeleitet werden und auch so im Browser erscheinen

Wird umgewandelt in den neuen zukรผnftigen URL Pfad

Umschreiben der URL mit einem rein numerischen Querystring

https://localhost:44396/test/keys?2618

Umleiten in ASP.Core .net5

In Asp Core wird die URL umgeleitet in der Startup.cs Datei.

Dabei wird ein app.Use( Loop Dateien) ausfรผhrt.

In folgenden Zeilen wird fรผr jede Datei der Pfad geprรผft. Das betrifft zunรคchst die Webseite selbst als html Aufruf, dann alle Images und Dateien wie javascript .js und .css

app.Use(async (context, next) =>

{

 Check URL and change URL

 

   // check next file Important

   await next();

 

}

 

 

 

 

Man kann mit 2 Weisen die URL umschalten

1.URL Rewrite

In der Rewrite Methode wird nur die URL nach aussen umgeschrieben und dargestellt

//option1: rewrite without changing visible URL

//context.Request.Path = sNewURL;

 

2.Response Redirect

In der Redirect wird dem Browser eine neue URL gesendet und die Seite erneut mit der neuen URL aufgerufen.

Der vorige Aufruf wird abgebrochen.

 

//note: .redirect dos not solve ~/

context.Response.Redirect(sNewURL, permanent: true);

 

 

 

Code in asp Core

Startup.cs

 

//------< redirect old folders and keywords in URL >------

//*loop through all website, images and files and check if it contains old url. redirect them permanent and break process

//*old url paths by title

//*like https://localhost:44396/Net-Framework/Files-Folders

 

app.Use(async (context, next) =>

{

    //--------< @Loop: html, images and files >--------

    string sURL_Path = context.Request.Path.Value;

    if (sURL_Path.Contains("."))   //*check for IsFile

    {

        //if file like *.js or *.css do nothing

    }

    else if (Regex.IsMatch(sURL_Path, @"=|๐Ÿ |๐Ÿ“š|๐Ÿ“„|๐Ÿ“‚|๐Ÿ“|๐Ÿ”Ž", RegexOptions.IgnoreCase))   //*if /๐Ÿ“š/๐Ÿ“‚=

    {

        //if redirect with parameters

    }

    //*default pattern: "๐Ÿ“„/{IDArticle}",

    else if (context.Request.QueryString.HasValue)

    {

        //----< Check for numeric ?123 >----

        string sQuerystring = context.Request.QueryString.Value;    //?1234

        string sNumericTest = sQuerystring.Substring(1);//1234

 

        if (float.TryParse(sNumericTest, out _))

        {

//-< isNumeric >-

string sNewURL = "/๐Ÿ“„/" + sNumericTest;

 

//context.Request.Path = sNewURL;   //*simple rewrite does not work

context.Response.Redirect(sNewURL, permanent: true);    //guide to new url

return;   // short circuit

          //-</ isNumeric >-

        }

        //----</ Check for numeric ?123 >----

    }

 

    else if (context.Request.QueryString.HasValue == false//*check for old URL-Folder Paths

    {

        //----< QueryParameters is empty not ?123 >----

        string sPath = context.Request.Path;

        if (sPath.StartsWith("/")) sPath = sPath.Substring(1);

        if (sPath != "")

        {

//-< change path to parameters >-

string sNewURL = "";

 

if (sPath.Contains("Search/", StringComparison.CurrentCultureIgnoreCase))

{

    string sSearchWords = sPath.Replace("Search/", "", StringComparison.InvariantCultureIgnoreCase);

    sSearchWords = Correct_old.decode_SemiCharactoers_in_Path(sSearchWords);

    //*old folders in path

    sNewURL = "/๐Ÿ“š?๐Ÿ”Ž=" + sSearchWords;

}

else

{

    //*old folders in path

    string sNewFolderPath = Correct_old.decode_SemiCharactoers_in_Path(sPath);

    sNewURL = "/๐Ÿ“š?๐Ÿ“‚=" + sNewFolderPath;

}

 

//option1: rewrite without changing visible URL

//context.Request.Path = sNewURL;

//note: .redirect dos not solve ~/

context.Response.Redirect(sNewURL, permanent: true);

return;   // short circuit

//-</ change path to parameters >-

        }

        //----</ QueryParameters is empty not ?123 >----

    }

 

 

 

    // next ! Important

    await next();

    //--------</ @Loop: html, images and files >--------

});

//------<( redirect old folders and keywords in URL >------

 

 

 

Folge: danach ist das Ergebnis faktisch von der neuen URL, also รผber den neuen Asp Controller und View

ABER die URL bleibt gleich

Context Request Path at debugging