0

I am looking to replace a part of a url with another part. For example

https://www.test.com/part1/a/b/c to https://www.test.com/part2/a/b/c OR

https://www.test.com/part1/part2/a/b to https://www.test.com/part3/part4/a/b

I basically want to be able to perform a find/replace on any part of a url. This will be used for a mass redirect in my custom route handler. The overall goal is to set this up via an admin page to do some custom routing.

Abhishek Duppati
  • 610
  • 6
  • 18

2 Answers2

0

You can try something like this with the help of Attribute Routing:

[HttpGet]
    [Route("students/{studentID}/courses")]
    public ActionResult GetStudentCourses(int studentID)
    {
        List<string> Courses = new List<string>();
        if (studentID == 1)
            Courses = new List<string>() { "ASP.NET", "C#.NET", "SQL Server" };
        else if (studentID == 2)
            Courses  = new List<string>() { "ASP.NET MVC", "C#.NET", "ADO.NET" };
       };
        else
            Courses = new List<string>() { "Bootstrap", "jQuery", "Angular" };
        ViewBag.Courses = Courses ;
        return View();
    }

This is how we navigate to URL example https://www.test.com/students/2/courses

If you want to have permalinks picked up from the root (site.com/{slug}) try these links Link-1 Link-2 If you're trying to change a part of URL in a link, try this Link

Abhishek Duppati
  • 610
  • 6
  • 18
0

I figured this out with a regular expression match and then replace.

An example

https://www.test.com/test-text/a/b/c to

https://www.text.com/new-text/b/c

Regular expression for this current redirect test-text/a(?s)(.*$)

replace new-text$1