Domain URL with Querystring without page name - c#

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");
}

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?

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?

How to change content based on the page name instead of querystring in asp.net

I've a site, we are using ASP.NET 4.0, and right now our products content is managed like this
www.franko1.com/products.aspx?serie=2000
where the querystring serie is the product ID, so its value is taken and then the contents is extracted from the database.
Now for SEO reasons, we've been asked to change the urls, so now they have to look like this:
What the boss want | Current Urls
--------------------------------------------------------------------------------
www.franko1.com/Relief_Valves | www.franko1.com/products.aspx?serie=2000
www.franko1.com/Inline_Flame_Arrester | www.franko1.com/products.aspx?serie=1000
www.franko1.com/Vent_Hatch | www.franko1.com/products.aspx?serie=3000
and so on ...
Right now, we are using a masterpage and the products.aspx and as I said, we take the querystring serie and we show the content based on its value, I have no idea how to do this using asp.net, I have read about ISAPI_Rewrite but I was wondering if there is a technique to approach this without dealing with the IIS server....
Well I don't know if I was clear, It is hard to explain,
No need for that. You can achieve this via routing (It's not just for MVC).
Routing has been available as a stand alone module for a while now, but with ASP.Net 4.0 you can now use routing for WebForms just as easily as you can with MVC.
You will need to add some routing to your Global.asax
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routeCollection)
{
routeCollection.MapPageRoute("Products", "Products/{Name}", "~/Products.aspx");
}
}
And with that you can now reference the route values in your page like so:
protected void Page_Load(object sender, EventArgs e)
{
string prodName = Page.RouteData.Values["Name"].ToString();
//Do lookup, etc...
}
Your URLs will end up looking like this:
www.domain.com/products/Relief_Valves
www.domain.com/products/Widgets
www.domain.com/products/TrashCans
etc..
Nice and easy... and clean!
URL rewriting was made just for exactly this purpose. The problem is that it really doesn't work with variable content directly appended to the root URL. There really isn't enough information for URL routing to separate such URLs from the rest of your URLs.
Would your boss accept www.franko1.com/p/Relief_Valves ?

ASP.NET 4.0 Routing and Default.aspx (How write A Handler? Is It Necessary Or Not)

i want to route Default.aspx to another URL when page starts.
my global.asax is like this :
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute(
"Default", // Route name
"My Site", // URL with parameters
"~/Default.aspx" // Parameter defaults
);
}
should i write a handler for my purpose?
(i found some samples for .net 3.5 and MVC but what about .net 4 web forms)
if yes how can i write it?
EDIT:
what this line exactly do?
routes.Add("Default", new Route(string.Empty, new RouteHandler("~/Default.aspx")));
i am using web forms -> Not MVC
thanks in advace
Here's a specific example of how to deal with routing on asp.net 4.0 web forms (it's just under the mvc part).
http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx
The way you are approaching it is fine. You do have an error in the second parameter of your route. Well perhaps not an error, I dislike spaces in urls as they are actually the encoded spaces. Check out the guide.
Just noticed your edit.
Adding the route essentially creates a mapping between a url or url pattern (which you have as string.Empty which is a problem) and a handler which serves the request(You specify RouteHandler which I don't believe actually exists?). .net Provides a PageRouteHandler which allows you to specify which page responds to your request and deal with a couple other details like security defined on the physical structure of your site. Internally, MapPageRoute is simply calling routes.Add but using the PageRouteHandler.

asp.net web form Custom Router Handler

I am using a custom route handler for a webforms application. I am using routes to determine localization. ie:
if the url has es or fr in the route it will load either spanish or french resources.
for example:
www.someroute/es/checkstuff/checkstuff.aspx
will load:
www.someroute/checkstuff/checkstuff.aspx with the spanish resources.
I am configuring the custom routes in global.asax via:
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
foreach (var value in _customRoutes)
{
routes.Add(value.RouteName, new Route(value.Route, new CustomRouteHandler(value.ResolvedRoute)));
}
}
where _customroutes is a list of routes.
Is there a way to do this with some kind of pattern matching so I can avoid adding a specific route for each page in the application. While I know I could use a t4 template to generate the routes, I guess I am looking for a dynamic way to create the list
I discovered that it was simpler to use MapPageroute than route.Add. With MapPageRoute I was able to use wildcards and with two entries:
routes.MapPageRoute("Spanish", "es/{*page}", "~/{page}");
routes.MapPageRoute("Kreyol", "fr/{*page}", "~/{page}");
I was able to provide the required routing for Spanish and Kreole pages.
Thanks to all for your help.
I would handle the language-part through some plain old Rewrite with an HttpModule in the BeginReguest handler and let the Routing engine take care of the rest.
Remember that the Routing mechanism takes place far later than BeginRequest so you can safely determine the language, set the CultureInfo on your thread and rewrite the request url to no contain the language-part, and your Routing would never know about it.

Categories