Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SEO URL rewriting ASP.NET

I already have an ASP.NET Web Site

I want to change my site to be more SEO url friendly.

I want to change ex. this site: www.mydomain.aspx?articleID=5

to: www.mydomain/article/learningURLrewrite - articlename needs to be read from DB

How do I accomplish this?

I have already tried with some articles from Google which mentions IhttpModule without any luck.

My goal is to have a class responsible for redirecting based on folderpath(like this):

string folderpath = "my folderpath" (could be articles, products etc.)
string id = Request.QueryString["id"].ToString();

if(folderpath.equals("articles"))
{
   string name = //find name from id in DB
   //redirect user to www.mydomain/article/name 
}

if(folderpath.equals("products"))
{
   string name = //find name from id in DB
   //redirect user to www.mydomain/products/name 
}

Also I want to remove the aspx extension

like image 234
Millerbean Avatar asked Nov 14 '22 16:11

Millerbean


1 Answers

You can use routing with ASP.NET WebForms too.

The steps are:

  1. Add the route (or routes) at application start.

    //In Global.asax
    void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.MapPageRoute("My Routename", "{*name}", "~/Article.aspx");
    }
    
  2. Create the Article.aspx as a normal webform

  3. In the code for Article.aspx, you can access the url path like this:

    public void Page_Load(object sender, EventArgs e)
    {
        var thePath = RouteData.Values["name"];
    
        // Lookup the path in the database...
    }
    
like image 180
Arjan Einbu Avatar answered Dec 06 '22 03:12

Arjan Einbu