Tuesday, December 23, 2014
How to hide aspx extension with url routing on asp.net 3.5 C#
After ton's of searching finally i got how to hiding aspx extension, of course on .net framework 3.5. Below here i will guide you how to step by step. Introducing with url routing, this is core of .net library which is function to masked your address into nice url/seo friendly.
First step add this .net reference to your web by clicking add reference
System.Web.Routing version 3.5.0.0
Next step go to your web.config, find and add
<httpmodules>
...
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</httpmodules>
And this
<system.webServer>
...
<modules>
...
<remove name="UrlRoutingModule"/><add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</modules>
...
</system.webServer>
It was done for adding library and web.config, now continuing for adding some code into your web application, next step we need to add a global.asax file into web application by adding new item -> global application class, now add a new method below Application_End
void RegisterRoutes(RouteCollection routes)
{
routes.Add(
"Client Data",
new Route("ClientData", new ClientDataRouteHandler())
);
//Another route
//routes.Add(
//"Task List",
//new Route("TaskList", new TaskListRouteHandler())
//);
}
Dont forget to add using System.Web.Routing on header global.asax, i was create method RegisterRoutes, first route has 2 paratemer, first parameter ex : "Client Data" to give a unique name for route instance and secound parameter for giving an alias to out aspx file, notice that we have class name ClientDataRouteHandler and we need create it first.Next create new class ClientDataRouteHandler in your solution in any directory
public class ClientDataRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return BuildManager.CreateInstanceFromVirtualPath("~/ClientDataForm.aspx", typeof(Page)) as Page;
}
}
Don't forget too, add using System.Web.Routing, System.Web.Compilation and System.Web.UI, the real aspx one was ClientDataForm.aspx but i was masked it into ClientData
Last step we need to call RegisterRoute function on Application_Start global.asax
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
Ok finnaly, try access your url with this route for example i will access with http://localhost:port/ClientData
Thanks, if you have any question please comment below
Subscribe to:
Post Comments
(
Atom
)
In my App have multiple pages..Can i take global class for multiple pages??
ReplyDeleteSure, you can return multiple instance for multiple page routing
Delete