13/11/2019 41

SEO Link with ASP.NET - ReWritePath Function

Hello everyone. In this article I will tell you how to make SEO compatible links with ASP.NET. So we'll make our links look the way we want. This event is called the ReWrite Path property. The RewritePath function makes redirects within the site. It makes the names of the pages within the sites easier to read and makes them suitable for SEO. It would be more useful for us to do this on one page instead of on each page. In ASP.NET, we use the ReWrite method to rewrite addresses. We can change the names of the pages with Querystring. This can make it easier for you to do your operations.  Table. To do this, you need to know the request information and where it will change. Because the same layout will work on the whole site and your whole system will convert your links with the same logic. We need to keep this conversion information in one file, link layouts and all information about conversion. This file may be in a database file. In our database you can find information such as page ID information and the address to be reprinted.
A table Example
Request page: /urunler/donanim.aspx
Source page: default.aspx?urun=donanim

Request Page: /urunler/anakart.aspx
Source Page: default.aspx?urun=anakart

Request page: /urunler/ram.aspx
Source page: default.aspx?urun=ram
The rewrite event is only a case with server-side code. That is, client-side transactions are not performed. Managing this type of process from a single file is the easiest process for our system. For this, you must have our file called Global.asax for the ASP.NET platform. With this file you can specify the code that will work on the entire site here and you can write here. This file contains some important functions that apply to the whole site. We can add commands to this file if we want. When we add a command, ASP.NET will find it and process itself. Example of Global.asax file

    void Application_Start(object sender, EventArgs e)
    {
	// Code that runs on application startup
    }

    void Application_End(object sender, EventArgs e)
    {
	//  Code that runs on application shutdown
    }

    void Application_Error(object sender, EventArgs e)
    {
	// Code that runs when an unhandled error occurs
    }

    void Session_Start(object sender, EventArgs e)
    {
	// Code that runs when a new session is started
    }

    void Session_End(object sender, EventArgs e)
    {
	// Code that runs when session is end
    }

    void Application_BeginRequest(object sender, EventArgs e)
    {	// We have created this method
        // Above functions are default with Global.asax
    }
This code represents the title of the Global.asax file and is created in the source folder of your project. The creation process is done automatically. Each time the site runs, the code inside this file will run. It will work in the command we wrote later. We can only add Application_BeginRequest to our Global.asax file above. The rest will automatically come with the file. Each of the event objects has a function and an internal variable to work with. When this variable function runs, it automatically goes into the function and we can use it. It is important to use it. BeginRequest Function: With this function we execute any request on ASP.NET pages. It does not matter what type of file it is. An image can be aspx file can be any thing hyani can. This function is called when the file is called. When the pages are called with the BeginRequest function, we will get the properties of the page called so that we can find out how and what to print the page as seo. Let me repeat that this function will work after the files that are called all over the site.
Application_BeginRequest method: C#

void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext context = HttpContext.Current;
    string adress = context.Request.Path.ToLower();
    int ext_index = adress.LastIndexOf(".aspx");
    if (ext_index == -1) //extention did not write on the system
    {
	return;
    }

    int last_content = adress.LastIndexOf("/content/");
    if (last_content == -1)
    {
	return;
    }
    int last_line = adress.LastIndexOf('/');

    string output = adress.Substring(last_line + 1, (ext_index - last_line - 1));
  
    context.RewritePath("~/default.aspx?file=" + output, false); //we have created the adress again
}
Using HttpContext.Current: With this method we are on the site ahngi page to return its content values. So this method allows you to access any value in the page Rewriting Our Reason: We produce pages again according to the value of the received page. For example, we can return the logic parameter with the query string. Finally, we create a new address link and print it in the address bar. This method is the dynamic one of the URLMapping event. With URL Mapping, we keep objects in our web.config file and rewrite the addresses according to the link from there. If the data is incorrectly entered, a 404 error will be received on the page. That is, if the generated page value is different from the above, a 404 error is received. To prevent this, we must enter our values correctly. That's all in this article. Keep following me. Burak hamdi TUFAN
Tags

Share this Post



Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment.

Comments

    There is no comment. Be the owner of first comment...