Brian
asked on
ASP.NET Web Forms URL Routing
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
Website Structure listed below:
default.aspx (main page)
about (folder)
author.aspx
bob.aspx
news (folder)
default.aspx
article.aspx
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>
Website Structure listed below:
default.aspx (main page)
about (folder)
author.aspx
bob.aspx
news (folder)
default.aspx
article.aspx
ASKER
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 runAllManagedModulesForAll Requests=" true" />
Not sure if this matters but as of now I'm using ASP.NET 4.0 Web Forms.
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 runAllManagedModulesForAll
Not sure if this matters but as of now I'm using ASP.NET 4.0 Web Forms.
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+.
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+.
ASKER
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.
So I just need to add http://mydomain.com/hailey and http://mydomain.com/author and it removes the .aspx file extension.
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");
}
So I just need to add http://mydomain.com/hailey and http://mydomain.com/author and it removes the .aspx file extension.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In your web.config file, did you added following elements or tags in <configuration> tags.
Open in new window