Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

URL Rewriting

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Frankie - 16 Sep 2007 03:21 GMT
I understand that with URL rewriting we can have a request come in for, say
Page1.aspx, and rewrite it so that PageA.aspx gets served up to the user.

My question (assuming the above is correct): What does the user see in the
browser after URL Rewriting? In the above case, when PageA is rendered to
the browser, will the address bar show "PageA.aspx" or would it show
"Page1.aspx" - the originally requested URL?

Thanks.
Cowboy (Gregory A. Beamer) - 16 Sep 2007 04:16 GMT
It depends on what you are doing.

If you are using a server.Transfer, they see the original URL. In general,
with an HttpHandler, it will also show the original URL. This is normal and
generally expected. If you want the new URL to show, you can program it that
way via a client side redirect, which can happen with Response.Redirect or
using certain other mechanisms that send out a redirect (generally a 300
level response).

Signature

Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************

| Think outside the box!

*************************************************
>I understand that with URL rewriting we can have a request come in for, say
>Page1.aspx, and rewrite it so that PageA.aspx gets served up to the user.
[quoted text clipped - 5 lines]
>
> Thanks.
Frankie - 16 Sep 2007 05:02 GMT
I'm not looking for the user to see/know the real page being rendered. What
I'm looking for is a level of indirection where the user never knows what
the "real" page is. Specifically, I'd like for the users to request a
particular page, say Page1.aspx, then on the server render PageA.aspx down
to the browser. All the while the user thinks they are looking at
Page1.aspx - which actually doesn't exist on the server - never has, never
will.

Apparently the default URL Rewriting behavior provided by
HttpApplication.Context.RewritePath yields this effect automatically.

Is this correct?

Thanks.

> It depends on what you are doing.
>
[quoted text clipped - 17 lines]
>>
>> Thanks.
Andreas Kraus - 16 Sep 2007 17:22 GMT
That's correct Frankie, Context.Rewritepath is the right class for that kind
of stuff.

For more info and examples check this:
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-a
sp-net.aspx


hth
Andreas
Signature

More on ASP.NET and Silverlight on my blog: http://www.andreas-kraus.net/blog

> I'm not looking for the user to see/know the real page being rendered. What
> I'm looking for is a level of indirection where the user never knows what
[quoted text clipped - 32 lines]
> >>
> >> Thanks.
Alan Silver - 16 Sep 2007 18:30 GMT
>That's correct Frankie, Context.Rewritepath is the right class for that
>kind of stuff.

However, you don't need to use HttpModules to do this. I have absolutely
no idea why people make URL rewriting so complex. Almost every article I
have seen insists on using HttpModules, when they just aren't needed.

Suppose you want the user to enter (or click) an URL like
http://www.domain.com/Toaster.aspx, and have the server send back the
contents as if they had typed (or clicked)
http://www.domain.com/products.aspx?product=Toaster
then all you need to do is add the following to your Global.asax file...

void Application_BeginRequest(Object sender , EventArgs e) {
  if (Request.Path == "Toaster.aspx") {
    Context.RewritePath("/products.aspx?product=Toaster");
  }
}

Note that this is air code, but it is basically right. You don't need an
HttpModule, just a bit of code in Global.asax

HTH

Signature

Alan Silver
(anything added below this line is nothing to do with me)

Cowboy (Gregory A. Beamer) - 17 Sep 2007 04:54 GMT
The main reason for HttpModules is efficiency. You can do it much simpler,
which is fine for many sites. But, if you are doing this long term on a site
that needs to scale, you are better to intercept the call as high up the
stack as possible.

Signature

Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************

| Think outside the box!

*************************************************
>>That's correct Frankie, Context.Rewritepath is the right class for that
>>kind of stuff.
[quoted text clipped - 19 lines]
>
> HTH
Alan Silver - 17 Sep 2007 14:17 GMT
>The main reason for HttpModules is efficiency. You can do it much simpler,
>which is fine for many sites. But, if you are doing this long term on a site
>that needs to scale, you are better to intercept the call as high up the
>stack as possible.

Is that why people use HttpModules? I never found a decent reason.

So what are the implication for doing it in Application_BeginRequest?
You imply it's less efficient, but why?

Thanks for the reply. Shame this point isn't made clearer. Everyone just
states that you have to use an HttpModule, without any explanation. At
least with your added info, an informed decision can be made.

Ta ra

Signature

Alan Silver
(anything added below this line is nothing to do with me)

Frankie - 17 Sep 2007 18:24 GMT
My motivation has more to do with driving the mapping logic from a database,
rather than hard-coding it into Global.asax (as per your earlier example).
Hard-coding is easier if the pages (or at least mappings) rarely change. But
in my case, I have admin pages whereby my customers can add entirely new
pages to their Web sites. Those pages never physically exist on disk, and
are instead defined in a database. When page requests come in, I have logic
that retrieves the "page" content from the database and injects it into a
placeholder within a "template aspx" page. That's an oversimplification but
gets you the basic idea. Anyway, my desire to do URL Rewriting comes from
the fact that users now request (perhaps via link on a page) these db-driven
"pages" with a long URL that looks something like
"TheirSite.com/SomeFolder/TheTemplate.aspx?pageID=398. The programming logic
then looks to the db and retrieves the page content for 398. Anyway I want
more "user friendly" URLs. So in the future the users will be able to
request, for example, "TheirSite.com/MeaningfulPageName.aspx". The URL
Rewriting logic will then map that friendly name to the real URL:
"TheirSite.com/SomeFolder/TheTemplate.aspx?pageID=398" URL.

-Frankie

>>The main reason for HttpModules is efficiency. You can do it much simpler,
>>which is fine for many sites. But, if you are doing this long term on a
[quoted text clipped - 12 lines]
>
> Ta ra
Alan Silver - 17 Sep 2007 22:01 GMT
>My motivation has more to do with driving the mapping logic from a database,
>rather than hard-coding it into Global.asax (as per your earlier example).
<snip>

My example was deliberately simple to illustrate the point. In reality,
this would be done from a database. Nothing that you have suggested
couldn't be done just as easily in global.asax, so I don't see that your
motivation affects the issue.

Ta ra

Signature

Alan Silver
(anything added below this line is nothing to do with me)

Cowboy (Gregory A. Beamer) - 17 Sep 2007 04:52 GMT
HttpHandlers can be really nice for this:
http://www.ftponline.com/vsm/2002_02/magazine/columns/qa/

Signature

Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************

| Think outside the box!

*************************************************
> I'm not looking for the user to see/know the real page being rendered.
> What I'm looking for is a level of indirection where the user never knows
[quoted text clipped - 32 lines]
>>>
>>> Thanks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.