Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ASP.NET Web Forms URL Routing

Posted on 2013-12-18
5
Medium Priority
?
1,177 Views
Last Modified: 2014-01-03
Hello Experts,

I'm using ASP.NET 4.0/4.5 Web Forms (Not MVC) for my website. I would like to be able to drop all of my pages file extensions that use .aspx. Is this possible using URL Routing within ASP.NET Web Forms and if so can someone show me how with code?

I found the following on a website but not sure why it's not working. Perhaps i'm missing something since I don't know what to do anyway. But this is what I have so far.

Global.asax


<%@ Application Language="C#" %>
<%@ Import Namespace="HaileyHugs" %>
<%@ Import Namespace="System.Web.Routing" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        //AuthConfig.RegisterOpenAuth();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("",
            "about/{action}/{categoryName}",
            "~/author.aspx",
            true,
            new RouteValueDictionary 
            {{"categoryName", "food"}, {"action", "show"}});
    }

    void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown

    }

    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs

    }

</script>

Open in new window


Website Structure listed below:

default.aspx (main page)

about (folder)
    author.aspx
    bob.aspx

news (folder)
    default.aspx
    article.aspx
0
Comment
Question by:asp_net2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 39727046
Hi,

In your web.config file, did you added following elements or tags in <configuration> tags.

<configuration>
      <system.webServer>
          <modules runAllManagedModulesForAllRequests="true"/>
     </system.webServer>
</configuration>

Open in new window

0
 
LVL 4

Author Comment

by:asp_net2
ID: 39727142
No, I did not have that in my web.config file but I did at it and I'm getting the following error message below and not no pages will load within my local IIS or even when I run the page from VS.

Error Msg:

HTTP Error 500.19 - Internal Server Error

The requested page cannot be accessed because the related configuration data for the page is invalid.

Error Msg points to this line:

<modules runAllManagedModulesForAllRequests="true" />


Not sure if this matters but as of now I'm using ASP.NET 4.0 Web Forms.
0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 39727653
Hi,

Undo my previous suggestion from your web.config file.

Use this link - Introducing ASP.NET FriendlyUrls - cleaner URLs, easier Routing, and Mobile Views for ASP.NET Web Forms

I would say Wow for the above link. It just take two minutes to solve your problem. I tested it and works fine from VS debugging.

But failed on IIS 5.1 on XP. Might work on IIS 6+.
0
 
LVL 4

Author Comment

by:asp_net2
ID: 39727701
Ok, little confused why you would need to do all of that. Since my last post I looked at a book called "Apress Pro ASP.NET 4.5 Csharp 5th edition" and found the following below which I had working now.

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.MapPageRoute("default", "", "~/default.aspx");
            routes.MapPageRoute("about-author", "author", "~/about/author.aspx");
            routes.MapPageRoute("about-hailey", "hailey", "~/about/hailey.aspx");
        }

Open in new window


So I just need to add http://mydomain.com/hailey and http://mydomain.com/author and it removes the .aspx file extension.
0
 
LVL 16

Accepted Solution

by:
Vikram Singh Saini earned 2000 total points
ID: 39728373
Ok, little confused why you would need to do all of that

Simply speaking to avoid writing custom map page routes for any of pages (or bunch of existing pages in website in any folder).

(1) Add line - RouteConfig.RegisterRoutes(RouteTable.Routes); in Application_Start in Global.asax.


(2)
Note the below single line -

public static void RegisterRoutes(RouteCollection routes)
        {
           routes.EnableFriendlyUrls();
        }

It will enable urls without .aspx extensions and same works nice for mobile version of website too.

Hope these are sufficient reasons. However you can go with your own custom way if you are happy with it.
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question