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