I am using ASP.NET web forms and have implemented routing for my website. I have a page named profile.aspx where I'm displaying user details. The current URL structure is http://localhost:56386/profile.aspx?username, but I would like it to work as http://localhost:56386/username to match the profile page.
This configuration works for the profile page routing, but it causes issues with other pages. I receive errors in the console related to ScriptResource.axd and my validations stop working.
Errors:
//VM83:1 Uncaught ReferenceError: WebForm_DoPostBackWithOptions is not defined
Uncaught SyntaxError: Unexpected token '<' (at WebResource.axd?d= getting this error when going to line number the code is
I've been struggling with this for a month, and any help or alternative suggestions to achieve the desired result would be greatly appreciated.
To achieve this, I added the following code to global.asax:
void Application_Start(object sender, EventArgs e)
{
RouteConfig.RegisterRoutes(System.Web.Routing.RouteTable.Routes);
}
And created a RouteConfig class in the App_Start folder as follows:
using System;
using System.Web;
using System.Web.Routing;
namespace ASP
{
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.Add("Profile", new Route("{id}", new PageRouteHandler("~/profile.aspx")));
}
}
}
Edit:
Add this line routes.Ignore("{resource}.axd/{*pathInfo}"); to RouteConfig will fix this error
public static void RegisterRoutes(RouteCollection routes)
{
routes.Ignore("{resource}.axd/{*pathInfo}");
routes.Add("Profile", new Route("{id}", new PageRouteHandler("~/profile.aspx")));
}
/profile/{id}or/p/{id}so that it can easily distinguish requests for profile pages from requests for other pages.