3
votes

I am trying to implement the url routing in my weforms. I've did it partially. Could you please help me to set the routing to the initial page or the index page. In my route map class I've written as following

routes.MapPageRoute("Index","","~/Index.aspx");

and I set as the Index.aspx as the set as start page and run it. But still the url is Index.aspx.

In MVC for the default page we will write the set of lines as below.

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Student", action = "Index", id = UrlParameter.Optional }
        ); 

In Webforms Is there any technique like this???

3
Have a look at a very good article by ScottGu here - Yogi
@gkrishy routes is the object of RouteCollection - Libin C Jacob
@LibinCJacob what you want probably can not done if you aren't using MVC, because in webforms there are are behind code with every View/Page unlike Controllers (in MVC) - manish
@ manish, Actually the routing is working for me. My problem is I can't set the default page routing in Webforms. - Libin C Jacob

3 Answers

2
votes

Try in this way,

Updated

  1. routeName: Name of the Route. Must be unique for each route. You can set any unique name, I have named it as Customers for better understanding.
  2. routeUrl: The Route URL you want to implement. For example here we want Customers.aspx to appear as only Customers (without .ASPX extension), thus this customization has to be defined here.
  3. physicalFile: The URL of the actual ASP.Net Page where the Route URL should redirect to. For this example it is Customers.aspx. The MapPageRoute method adds the Route to the Global Route collection i.e. RouteTable.Routes. The RegisterRoutes is called inside the Application_Start event handler so that all the routes are added the RouteTable collection when the application starts. That’s all you need to do the remove or hide the .ASPX extension of the ASP.Net Web Page, now if you type in the URL as http://localhost:1932/RoutingCS/Customers/ or http://localhost:1932/RoutingCS/Customers both will redirect you to the customers page i.e. Customers.aspx.

    protected void Application_Start(object sender, EventArgs e)
    {
      RegisterRoutes(RouteTable.Routes);
    }
    
    public static void RegisterRoutes(RouteCollection routes)
    {
      routes.MapPageRoute("Index","Index","~/Index.aspx");
    }
    
0
votes

For steps Click Here

Walkthrough: Using ASP.NET Routing in a Web Forms Application

0
votes

I don't think this can be done using the new routing techniques only, but you still can do it by specifying the default page in your web.config. This can be done by adding the following as a child element of the <configuration> node (almost at top level) :

<system.webServer>
    <defaultDocument enabled="true">
        <files>
            <clear />
            <add value="Index.aspx" />
        </files>
    </defaultDocument>
</system.webServer>

Nevertheless, your url will display as yourdomain.com and not as yourdomain.com/Index.aspx

Another alternative is to have your Default.aspx page do a permanent redirect to Index