13 July 2017

How to add friendly URL to your ASP.NET web application

First you need to install a NuGet package called Microsoft.AspNet.FriendlyUrl.

Then, add a class called RouteConfig.cs and make it similar to the codes below:

using System.Web.Routing;
using Microsoft.AspNet.FriendlyUrls;

namespace WebFormsIndentity
{
    public static class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            var settings = new FriendlyUrlSettings();
            settings.AutoRedirectMode = RedirectMode.Permanent;
            routes.EnableFriendlyUrls(settings);

            routes.MapPageRoute("", "MyPage", "~/Page.aspx");
        }
    }
}

**Actually, you can add as many MapPageRoutes as you want in RegisterRoutes. JUst keep the first and second parameters unique.

Finally, in your Global Application class (Global.asax), add the following in the Application_Start() event:

 protected void Application_Start(object sender, EventArgs e)
 {
     RouteConfig.RegisterRoutes(RouteTable.Routes);
 }


And TA-DA.... you now have an SEO-friendly URL!

No comments:

Post a Comment