How to remove .aspx extension from url

How to remove extensions from a page url in C #.

eg: questions/ask.aspx

I need the url of my web application in the following format:

questions/ask

      

If anyone has an idea, then pleading guides me ...

+3


source to share


2 answers


If you are using web forms you need to add a custom router handler using URL Routing in the file Global.asax

. Check out this sample:

Global.asax



public class Global : System.Web.HttpApplication
{

    //Register your routes, match a custom URL with an .aspx file. 
    private void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("About", "about", "~/about.aspx");
        routes.MapPageRoute("Index", "index", "~/index.aspx");
    }

    //Init your new route table inside the App_Start event.
    protected void Application_Start(object sender, EventArgs e)
    {
        this.RegisterRoutes(RouteTable.Routes);
    } 
}   

      

+4


source


You need to complete URL Rewriting

URL rewriting is the process of intercepting an incoming web request and redirecting the request to another resource. When doing URL Rewriting, typically the requested URL is checked and based on its value, the request is redirected to another URL.

You can add this to Web.Config



<urlMappings enabled="true">
 <add url="~/questions/ask" mappedUrl="~/questions/ask.aspx?page=Ask"/>
</urlMappings>

      

See here

+2


source







All Articles