|
Nice article
I face one problem while rewrite url when i rewrite url css and image is not apply on my page i also write step1 line on page load of my main page but it's not working can u please give my some solution i am working on .net 2005 so on my local server how to configure iis.
|
|
|
|
|
Hi thanks for the post.
I have been working on this for a few hours and was pleased to see someone else was working toward a solution. The key for me is getting all the other resources (css, js, images, etc.) to map with those resources having relative paths.
I made a small vs project using your files and got your code to work fine. The only problem (and it's a big one of course) is that it doesn't look like other resources like css files and image files will work if they have relative paths.
For example when I added an image to both the Default.aspx file in your sample and also the same image (with the same path) to the ShowContents.aspx file, the image link is broken in the ShowContents.aspx file when it is requested via the rewritten url.
I'm sure I'm missing something. Any help will be appreciated!
|
|
|
|
|
Hi,
1)
If you start your image path with a forward slash like this
"/images/abc.gif"
it will work from root directory.
2)
To use relative path you should also have configured your IIS to serve all requests from ASPNET.dll.
and
3)
You must have
System.IO.Context.RewritePath(Path.GetFileName(Request.RawUrl));
above line in the Page_Load() event of your main page.
If above does not help, please post your code here or email me.
Thanks for visiting this and leaving a comment.
Thanks
Manu Agrawal
|
|
|
|
|
Hi
GOD Bless you
I have this in my dreams
ASP.NET SEF URL is Very Easier Than PHP & Apache
Never Forget you for this Article
Thanks a Lot!
Majid
|
|
|
|
|
why do i lose all my images and my css ????
|
|
|
|
|
Hi,
Did you use the code in Step 1 above?
Please try step 1 and then let me know if you still get problems.
Thanks.
|
|
|
|
|
well , i didn't do that before , now i did STEP 1 , but it still not working well ....
i just want ot point out that im using masterPages , but i guess it does not matter
|
|
|
|
|
i have to end the request url with .aspx or .html ... etc ??
i saw some urls that look like that : www.abc.com/abc (not like www.abc.com/abc.aspx)
but it seems that global. don't get the request if it looks like this : "www.abc.com/abc " .
any idea ???
about the css problem , i just use the full path (http://www ....) not like ~/css/general.css ....
Thanks .
|
|
|
|
|
Till IIS 6, only asp.net requests (e.g. .aspx, .ashs, .ascx etc.) reach to global.asax file.
If you have permissions then, you can set IIS to handle .htm, .gif etc. requests by the process that handles asp.net.
1) Open IIS admin and right click on the website you want
2) select Properties > Home Directory > Configurations
3) select the .aspx from the list, click on Edit, and then copy the process path.
4) Cancel
5) click Add then paste this path in to the top box
6) type .htm in the extension box, uncheck Verify File exists
7) Click OK
Repeat this process for any other file extensions you want like .doc, .ppt etc.
|
|
|
|
|
yeah , i got that by the time u reply ... but im in shared host account , can't do much
Thanks alot .
|
|
|
|
|
You should check out also DotNetNuke's friendly url implementation as it provides nice framework for such functionality. One feature that I personally like is the fact that you can create a friendly url with custom parameters like "/Home/MyPage/para1/50/para2/hello" so that the "para1" maps with value of "50" and "para2" maps with value "hello" and they are accessible through normal Request.QueryString -object.
|
|
|
|
|
What if let's say you have a GridView with paging enabled and more than a page of rows in the GridView. In a "fake URL" page.
Now you will push on the "2"-nd page link on the GridView pager. Things will get a bit nasty then..
The Request will hold a different URL like "http://machine/fakeDir1/fakeDir2/ShowContents.aspx?querystring..."
I simply can't make it look nice (it works but after the first postback the URL in the browser looks ugly) after the first postback
|
|
|
|
|
Watch out if you have a validator in any of your pages because they will request a javascript throught the WebResource.axd and if in "Application_BeginRequest" you treat this as a fake URL the page will not get its javascript but whatever you serve it..
Take care as this took me 1/2 hour to figure out ..
|
|
|
|
|
Thanks for your feedback.
Really a useful tip.
Cheers.
|
|
|
|
|
For now I just put the following line in Application_BeginRequest event handler:
if (File.Exists(Request.PhysicalPath) ||
Directory.Exists(Request.PhysicalPath) ||
Request.AppRelativeCurrentExecutionFilePath.ToLower().EndsWith(".axd"))
{
return;
}
|
|
|
|
|
Hello, great post ...
I do not know if possible, use this technique for virtual domains like as http://virtual.domain.com
Can you help me ?
|
|
|
|
|
Yes, you should be able to use this with subdomains like http://virtual.domain.com without any problem.
Same code will work for you.
Thanks for your feedback.
Manu
|
|
|
|
|
To all you guys using various string functions (IndexOf, Split, etc) to find your query strings and Url parts, stop! Just use a System.Uri and your done! It already parses everything for you. You may have to split the Query Strings, but at least it does the heavy lifting for you.
Thanks.
|
|
|
|
|
Isn't the "Custom HttpHandler" the correct (best practice) way of doing this? What advantages are in your method? Did you try HttpHandler and not get it to work? (Cuz that's what I did)
|
|
|
|
|
Hi Jasmine, Thanks for your response.
An HttpHandler will take more system resources as it will listen to more requests, while only .aspx requests will be handled by global.asax file.
Please let us know if you have additional information.
Your feedback is appreciated.
Manu Agrawal
|
|
|
|
|
That is a very good point - anything that doesn't have to go through that ASP engine, is a Good Thing. In your case, adding an HttpHandler is simply overkill - all the things you want to alter are already going through the ASP engine. Mine actually responds to all requests that aren't aspx pages, and now that I think of it, that's why I didn't use something similar to your method in the first place. I had a slightly different requirement than your site. In my case, I wanted to handle all files with my custom handler, so I had to re-map the file type *.* to be handled by the ASP engine. This means all requests to my site are going through the ASP engine. That's a clear performance problem, but it adds the ability for me write custom file handling code very easily. Your method is better, if the files you are handling are aspx files, but what would you do if you needed to serve up music, video, and various application-specific documents (like .doc, .ppt, etc)?
The reason I ask is because it took me a long time to get the HttpHandler to work correctly, and nobody out there was able to help me. I finally figured it out myself, but I was amazed at the sheer number of people who basically said to do it another way. Your method seems to be a popular alternative, and one that most people seem to use when they find out that HttpHandlers are tricky to get right.
(I'm basically building a compliance system... such that users from different companies can only get the documents that are approved by their compliance department. There is a possibility of us having multiple versions of the same document, one for each company, but we want the URLs to be the same so people can share them between companies, and everyone still sees the correct versions. We want the server to serve up the correct version based on the user's login, and we wanted to send back an error if the person is not logged in, or if the company does not have an approved version of the requested document. This had to work with all requests for the document, regardless of the actual URL used.)
|
|
|
|
|
Hi,
It's not strictly true that an HttpHandler will consume more resources - the handler will only listen to the type of resource it is written to process, so in the case of rewriting (say) *.html requests, the handler would only be invoked for those requests. One of the BIG advantages of using an HttpHandler is that we can create friendly postbacks to the page that dont contain the target scripts query strings or rewritten resource name.
Check out my article here[^] for more information on this subject.
--- Regards Steve
|
|
|
|
|
|
To all you guys using <urlMappings> and having problems with postback and/or AJAX UpdatePanel... I ran into a bug about the action of the form. It will redirect on the postback or callback to the redirected URL instead of the original URL. The next piece of code will fix this:
private void FixUrlMapping()
{
string clientFilePath = Request.RawUrl;
int index = clientFilePath.IndexOf('?');
if (index >= 0)
clientFilePath = clientFilePath.Substring(0, index);
if (clientFilePath != Request.CurrentExecutionFilePath)
{
// This request is done using a mapped URL. This has set the action of the html-form (<form action=...>) back
// to the physical aspx file. This results in a navigation problem navigating away from the mapped url to
// the physical url.
// For example: whithout this code, a postback from /Quote/MyQuotes.aspx would result in the URL /Order/MyOrders.aspx?Type=Quotes
// This is not a problem for the actual order/quote page, but the navigation will point to Order/MyOrders.aspx.
// Thus the crumbtrail, menu and URL are misleading.
// Fix: Change the 'action' of the form back to the virtual admin page
string action = clientFilePath;
index = action.LastIndexOf('/');
if (index >= 0)
action = action.Substring(index + 1, action.Length - index - 1);
string script = string.Format("theForm.action='{0}';", action);
Page.ClientScript.RegisterClientScriptBlock(typeof(string), "ResetAction", script, true);
// Bugfix for UpdatePanels. The call-back would reset the action of the form.
script += string.Format("theForm._initialAction='{0}';", action);
ScriptManager.RegisterClientScriptBlock(Page, typeof(string), "ResetAction", script, true);
}
}
Marco Vervoort - Pulse Business Solutions
|
|
|
|
|
Hi;
Though your post does not relate to this article in particular, still it is good and thanks for that.
This article is about NOT having to use <urlmappings> in the Web.Config; It explains how to map URLs that physically do not exist (where contents come from a database).
Thanks again for your interest.
|
|
|
|