URL Rewriting not working in global.aspx file - c#

I want to make some friendlyurls in my ASP.Net C# project and I'm trying to do this in global.asax file and protected void Application_Start(object sender, EventArgs e) but I am getting error in in browser. it is not working. I am attaching screenshot and pasting code also with this.
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("Dashboard", "{FullName}-{Id}/Dashboard", "~/Dashboard.aspx");
routes.MapPageRoute("Reviews", "{FullName}-{Id}/Reviews", "~/Reviews.aspx");
routes.MapPageRoute("Events", "{FullName}-{Id}/Events", "~/Events.aspx");
}
This is my code that i am using in my global.aspx file under the Application_Start method. by using this code, it is making url correct but not redirecting on any page and showing error is The: localhost page isn’t working
localhost redirected you too many times.
this error i am getting on the browser while i am running my project.
This is my problem please anyone resolve this problem and help me.

Just a stab...Not too sure if you can have parameters "dash" separated...have you tried.
routes.MapPageRoute("Dashboard", "{FullName}/{Id}/Dashboard", "~/Dashboard.aspx");
routes.MapPageRoute("Reviews", "{FullName}/{Id}/Reviews", "~/Reviews.aspx");
routes.MapPageRoute("Events", "{FullName}/{Id}/Events", "~/Events.aspx");
Also, your path is localhost/user/<URL Route>...not too sure if it will work without the rest of the path in the routes.
Can you confirm your landing pages are being hit (put break in page_load)?

Related

How can I pass a query string in a route in a web forms application

I have a database that returns data based on the query string. My url is formatted like this
www.example.com/CompanyPage.aspx?id=(some number)
I want to set up routing so that I can pass a human friendly url instead
www.example.com/CompanyPage.aspx/(company name)
I inserted a Global.asax file into my web forms project, and set up routing like this.
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("Burger_Hut", "CompanyPage.aspx?id=32", "~/CompanyPage.aspx");
}
The problem is that I get an error stating that
The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.
Parameter name: routeUrl
All my googling takes me to how to fix this via MVC. But this is not an MVC project. It's a webforms project. Can someone point my in the right direction on this? Is routing the proper way to go about doing this?

Domain URL with Querystring without page name

I have a domain mydomain.com which hosts asp.net website. I am creating some dynamic user's page on the company specific. If i open any page so it will show like mydomain.com/career.aspx?pagename=testpage.
But I want to change my URL and make it user friendly like mydomain.com/testpage1 or mydomain.com/testpage2 etc...
Can you anyone please explain in a step by step to implement this?
You need to look into Asp.net routing. I'd start here:
Walkthrough: Using ASP.NET Routing in a Web Forms Application
Esentially, you'll end up putting something like this in your Global.asax:
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("Customers", "Customers", "~/Customers.aspx");
}

URL Routing .Net 4.5 Webforms

I have a webforms app that I would like to add some routing in so when a user types in www.mySite.com/Brd it will take them to a specific page. I can get this to work if I put an argument in, however I don't want any. Here is what I have in my application start method
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoute(System.Web.Routing.RouteTable.Routes);
}
void RegisterRoute(System.Web.Routing.RouteCollection routes)
{
routes.MapPageRoute("Route1", "Rep", "~/SalesRep/SalesRepHome.aspx");
routes.MapPageRoute("Route2", "Brd", "~/Board/BrdLogin.aspx");
}
The route for the Brd takes me to www.mysite.com/BrdLogin.aspx without the subdirectory and the Rep route does nothing. Could someone point me in the right direction?
Try adding this line to your RegisterRoutes method...
routes.EnableFriendlyUrls();
Make sure you're also referencing the Microsoft.AspNet.FriendlyUrls assembly
Also, try putting a forward slash after Rep and Brd e.g.
routes.MapPageRoute("Route1", "Rep/", "~/SalesRep/SalesRepHome.aspx");
routes.MapPageRoute("Route2", "Brd/", "~/Board/BrdLogin.aspx");

updatepanel not working Url routing

I'm running into a bit of an issue. My postbacks are causing my asp.net application pages to jump back to the top of the page even though they are inside update panels.
when i use this routing updatepanel stop working
void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("UProfile", "{ID}", "~/UProfile.aspx");
}
but when i use this code it works fine, but i want the simplest URL
void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("UProfile", "Users/{ID}", "~/UProfile.aspx");
how should i solve this problem ?
I have tried a couple different solutions, that did not work:
1) http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
2) http://www.iis.net/learn/extensions/url-rewrite-module/url-rewriting-for-aspnet-web-forms
This will route will target root Url because there is no any specific identifier to map it properly. I will rather suggest to write a route constraint so you can decide when to use the user details page after validating the passed parameter.
You can add routers in Asp.net Webforms too. Check the following link.
http://www.shubho.net/2011/02/aspnet-mvp-url-routing-webforms-part3.html
The possible solution would be...
RouteTable.Routes.MapPageRoute("UProfile",
"{ID}",
"~/UProfile.aspx",
false,
new RouteValueDictionary { { "ID", "1" } }, new RouteValueDictionary { { "ID", "[\d]+" } });

404 error with MapPageRoute() routing a aspx page within a folder

Trying to route in webforms, getting a 404.
I have set up my global.asax.cs file as follows using System.Web.Routing;
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
private void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("ProfilePage",
"Profile",
"~/Manager/Profile.aspx");
}
profile.aspx is located within the manager folder. No idea why it's not working. Would be grateful if someone could make some suggestions I am fairly new to asp.net.
I am expecting the url localhost:60008/Manager/Profile/ to load the Profile.aspx page.
The second parameter specifies the URL. Try:
routes.MapPageRoute("ProfilePage",
"Manager/Profile",
"~/Manager/Profile.aspx");
You have it right the first time. Here a valid routing
routes.MapPageRoute("", "YourPage", "~/Your/full/url.aspx", true);
The first para can be let empty, the second is the one you are going to use, the third is the url of your page, the last [optional] is to check if the file exist physically or not.
I believe where the error comes is how you use it, for a hyperlink you would say
NavigateUrl="~/YourPage"
In a html anchor
href="~/YourPage" runat="server"
In the browser address bar it will show like this http://YourDomain.com/YourPage/
That's the way it works for me. Personally having to put the folders in there defeat the purpose of using routing no?

Categories