mapRoute to a Web Form using ASP.NET 4 - c#

I have a Url like this
http://localhost:4737/Site/listing/NH/Plaistow/2831516
and I want it to reroute to
http://localhost:4737/Site/listing.aspx
I was reading how to do this for Web Forms here
https://web.archive.org/web/20211020111718/https://www.4guysfromrolla.com/articles/012710-1.aspx
Here's what my route looks like.
routes.MapRoute(
"FriendlyUrl",
"Site/listing/{state}/{town}/{mlsnumber}",
"~/Site/listing.aspx");
In my listing page I plan on accessing the following variables
Page.RouteData.Values["state"]
Page.RouteData.Values["town"]
Page.RouteData.Values["mlsnumber"]
But when I navigate to http://localhost:4737/Site/listing/NH/Plaistow/2831516,
I just get a HTTP 404 error.
I know how to get this working with MVC, but this is a fairly large application, all written with web forms, so rewriting isn't feasible.
Any ideas on how to troubleshoot this would be helpful.
Thanks !
Here is the working code. Thank you to mrchief for helping me resolve this.
routes.MapPageRoute(
"FriendlyUrl",
"listing/{state}/{town}/{mlsnumber}",
"~/listing.aspx");

Yore doing it the other way. If you're using WebForms, you need to implement UrlRoutingModule as shown here: https://web.archive.org/web/20201205221404/https://www.4guysfromrolla.com/articles/051309-1.aspx
The Routing Rules were designed for use in ASP.Net MVC applications where you redirect a Url to its appropriate Controller (Page in WebForms) with action params (query params in WebFroms parlance).

Related

Could someone explain how this ignore route works?

So I added an ASMX web service to my MVC4 but when I tried to access it I got a "The Resource could not be found" error. After searching I found the answer here.
In short, I had to add the following IgnoreRoute to my RouteConfig file.
routes.IgnoreRoute("{*x}", new { x = #".*\.asmx(/.*)?" });
I understand the MapRoute function in MVC fairly well, however the IgnoreRoute, not so much. I understand that it's targeting the .asmx postfix but I'm not sure on the how's and why's of this quick fix.
How does this IgnoreRoute work, and exactly why does it make my MVC app magically understand how to find and execute my web service? BTW, My only mapped route, currently, is the default, but is there another/better way of solving this issue using MapRoute or another fix?
ignore route indicates that routing should ignore these requests and ASP.NET processing of these requests will occur.
http://haacked.com/archive/2008/07/14/make-routing-ignore-requests-for-a-file-extension.aspx/

Custom URL making- URL rewrite

My URL is "http://properties.Poject.net/Public.aspx?account=ACCOUNT&id=HL101"
and my desired URL is http://properties.Poject.net/ACCOUNT/HL101
Please help me to achieve this. ALso i don't have URLWrite option in my IIS 6 and my project is under .net 3.5. Please help me config in Web.config
Thanks.
Manu
Use the concept called Url Routing in asp.net
refer article
http://www.asp.net/web-forms/tutorials/aspnet-45/getting-started-with-aspnet-45-web-forms/url-routing
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
use the following two links, it briefly describe how to apply url Routing,by the way URL routing is more advance concept then URL rewriting so use that.....
https://web.archive.org/web/20211020111718/https://www.4guysfromrolla.com/articles/012710-1.aspx
http://msdn.microsoft.com/en-us/magazine/dd347546.aspx

URL Routing Encoded URLs in web application

My goal is to move away from an ISAPI filter that was set up and instead do all the rewriting/routing at the application level.
I have URLs like: product.aspx/2008C20080929.htm
I can correctly route those pages using the following RouteTable.Routes.MapPageRoute:
RouteTable.Routes.MapPageRoute("testRoute", "product.aspx/{page}", "~/routeTest.aspx");
However, some of the other URLS are "encoded", example:
product.aspx/%255COH%255C2008%255C20080929.htm
I am unable to route these pages with the previously stated route. Is this not possible? I'm getting 400 bad requests.
Instead of using URL Routing, I used IIS7's Rewrite Module
URL Rewrite

url rewrite for aspx page

I have a page, called foo.aspx and i d like to rewrite the url as bar.something
How to do this? How does url rewrite happens in asp.net
Should i create a generic handler?
or should i get some url rewrite modules and add to app?
This is done by configuring IIS, and will require IIS7, look here for configuration help:
http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/
Remember, you also have the new Routing option with ASP.NET 4.0:
https://stackoverflow.com/questions/90112/iis-url-rewriting-vs-url-routing
You should also check this SO response:
IIS URL Rewriting vs URL Routing
Some basic info on the differences between URL Re-Writing and Routing:
http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/

Rewriting URLs in ASP.NET?

I am using ASP.NET C#.
How do I implement URL re-writing procedure that is similar to StackOverflow.com?
http://stackoverflow.com/questions/358630/how-to-search-date-in-sql
Also, what is the meaning of values such as "358630" in the URL? Is this the question ID (the basis for which they use to fetch the data from the table)? Whatever it is, in my application I am identifying records using an "ID" field. This field is an identity column in an SQL table. Right now, my URLs are like the following:
http://myweb.com/showdetails.aspx?id=9872
But I'd like them to appear like:
http://myweb.com/showdetails/9872/my_question_title
Or:
http://myweb.com/9872/my_question_title
Or whatever the best way, which will taste good to search bots.
My application is hosted on Go Daddy's shared hosting service, and I feel that no customized ASP.NET "HTTP module" or no customized DLL for URL re-writing is working on their server. I tried many samples but no luck yet!
I found that Stack Overflow is hosted on Go Daddy (shared hosting?). Maybe Stack Overflow's method will work for me.
SO is using ASP.NET MVC. You really need to read in details how MVC URL rewriting works, but the gist of it is that the 'questions' part in the URL is the name of the Controller class (which roughly corresponds to the 'showdetails' in your URL) and the number is a ID parameter for the default action on that Controller (same as the parameter 'id' in your URL).
Since MVC isn't an option you can try redirecting the 404s. This will work in ASP.NET 1.1 and above: Redirect 404s and 405s to your own handler using either IIS config or web.config, parse out the request in the handler and redirect to the appropriate resource.
<configuration>
<system.web>
<customErrors mode="On" defaultRedirect="error.html">
<error statusCode="404" redirect="newHandler.aspx"/>
</customErrors>
</system.web>
</configuration>
Before the advent of System.Web.Routing, the common practice was to use UrlRewriter.NET. Worked well enough, but could bite you when configuring IIS. I'm not sure if there are any simple ways of using the new Routing classes in ASP.NET (i.e., drop it in and go vs. refactoring code).
please explain the meaning of values
such as "358630" in the URL
That is (presumably) the ID for the question in the database. In the MVC model
myurl.com/questions/358630
is analogous to
myurl.com/questions.aspx?id=358630
The question title on the end of the URL is actually being ignored by the app. It's generally "tacked on" for search engine optimization and human readability purposes. In fact, you can change the title of this question in the URL and notice the page still loads just fine.
The new System.Web.Routing dll is part of ASP.NET 3.5 SP1, and is bin deployable on ASP.NET 3.5, so you could use the features of that on a classic ASP.NET WebForms site.
You'll probably want to take note of Phil Haack's comments in his post on using MVC on IIS 6 as you'll probably need to include the .aspx extension in your routed urls
http://www.mysite.com/controler.aspx/action/id
You might also want to check out Questions Tagged SEO.
The ignored question name at the end of the url is often called a "Slug", and is used for SEO purposes to include the page title in the url.

Categories