Freelance Project Requests info@CodeDocu.de Software Development in C# WPF Asp.Net Core Vba Excel Word SQL-Server EF Linq, UWP Net
#

 

How to set URL paths in ASP Core. Redirect Net5 to a different path?

 

From an old application, the URL addresses should be redirected to new paths in the Asp Core application.

The URL stored in Google and other SEO search engines should not be lost.

 

 

procedure:

 

When entered in browser

If an old URL is entered, it should be redirected to a new address

 

After redirection

After entering the link, the path will be redirected to a new ASP MVC address according to new specifications and will also appear in the browser

Converted to the new future URL path

 

Rewrite the URL with a purely numeric query string

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

 

Redirect to ASP. Core .net5

 

In Asp Core, the URL is redirected in the Startup .cs file.

An app. Use( loop files).

The following lines check the path for each file. This applies first to the website itself as an html call, then to all images and files such as javascript .js and .css

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

{

 Check URL and change URL

 

   // check next file Important

   await next();

 

}

 

 

 

 

You can switch the URL with 2 ways

1.URL Rewrite

In the rewrite method, only the URL is rewritten and displayed externally.

//option1: rewrite without changing visible URL

//context.Request.Path = sNewURL;

 

2.Response Redirect

In the redirect, a new URL is sent to the browser and the page is called up again with the new URL.

The previous call is canceled.

 

//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 >------

 

 

 

Consequence: after that, the result is de facto from the new URL, i.e. via the new Asp Controller and View

BUT the URL remains the same

 

 

Context Request Path at debugging

 

 

Mobile

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de