Url rewriting with asp.net. is there a configuration needed? - asp.net

I'm trying to enable rewrited urls in my project.
it's very good described in this post: urlrewriting by scottgu
It works very well when im running it on localhost, but as soon as i upload it to my host (.net 3.5), it doesn't work! i always get redirected to a 404 page!
Is there a configuration needed to enable this?
as scottgu says no, but i don't find out why it's not working...
thanks
// UPDATE 2.09.2010
Is there actually a way to enable routing or rewriting without having iis7 or the ability to install a modul like ISAPI Rewrite on the server?
Looks like i got a bad asp.net host...

In your localhost environment you are probably running the website on your ASP.NET Development server. That server is set up to capture all request (* . *) and run them through the ASP.NET pipeline.
II6 on the other hand, is configured to only send some requests ( ie *.aspx, *.asmx, *.ashx) through the ASP.NET pipeline. So if you are trying to catch a request for an url like "/my/fine/url" that will never be passed to the ASP.NET handler, and thus not rewritten.
You can change this configuration in the Application configuration for the website:
Open IIS Manager and right-click on the website, choose Properties
On the tab "Home Directory", click "Configuration..." button.
Click "Insert..." button to insert a Wildcard application map.
In "Executable:" insert path to aspnet_isapi.dll, in my case C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll (note: this path may differ on you server).
Remember to uncheck "Verify that file Exists"
Click OK!
And so! All your requests should now be directed to the ASP.NET handler and hence caught in your URL rewriter, regardless of extension.
But I must admit that I'm a bit unsure as to how this will affect performance on you site, routing all requests for static files, css, images etc through the ASP.NET handler. Maybe someone else out there has something to say about that.
/Dennis :-)

There are two ways to get the extensionless routes in IIS6:
a) ISAPI rewrite or other ISAPI url rewriter
b) Use a wildcard mapping to aspnet_isapi.dll
See this blog post for detailed instructions.

Here is example how to use new System.Web.Routing within ASP.NET WebForms.
http://deepumi.wordpress.com/2010/02/27/url-routing-in-asp-net-web-forms/

Related

Extensionless URL in ASP.NET 4.0 on IIS6

I have a ASP.NET 4.0 WebForms webapp running on a IIS6 webserver. I'm not allowed to make any changes to the webserver. I have a flex app embedded in this file called:
myapp.contoso.com/mysubapp/mysubapp.aspx
I'd like to only require the user to use the URL:
myapp.constoso.com/mysubapp
to reach the application and essentially hid the mysubapp.aspx permanently. I've been checking out URLRewrite and URLRewriting.net... It all looks to be a little much for this once instance in which I need it (if I need to add more rewrites in the future I'll use one of those frameworks). Is there a simple way to achieve this? I've checked out similar posts... it seems that I may need to write a simple one myself?
URL Rewriting using iis6 with no extensionless urls
How to deal with extensionless Url in ASP.Net and IIS6
The simplest thing I can think of is to rename your web page from mysubapp.aspx to default.aspx. This will allow users to request myapp.constoso.com/mysubapp and get your page. This should work if you have not removed default.aspx from the default document list in IIS.
Alternatively, you can add mysubapp.aspx to the list of default documents in IIS.

URL Redirects in ASP.NET

Ok, wierd problem I cant figure out. Hopefully someone where can. I have inherited a site that was developed with a very over-architected Content Management System. I am having problems now with the redirection functionality built into it.
This is on a dedicated Windows 2003 server running ASP.NET 3.5 sp 1. The redirects are stored in the database, and I have confirmed that the correct redirect is in place in the database. Finally, the file extension .html has been mapped in IIS to the ASP.NET ISAPI. And there is an HttpHandler created to redirect the .html requests. The default documents on the server, in order, are:
default.aspx
index.aspx
default.asp
index.asp
default.html
index.html
for this example, we have two redirects both pointing to the same content page. /example and /example.html
when requesting /example.html it correctly finds the appropriate redirect in the database and does its magic. Bueno. When requesting /example it gives a 404 page. Its not even the asp.net yellowish 404 generic error page. Its the standard vanilla IIS 404 response so it appears that asp.net is not intercepting these requests.
Let me know if any other information is requested and I will try to provide what I can. Thanks in advance for all the great recommendation I am sure will come from the community.
You should be able to map a wildcard extension to go through the ASP.Net ISAPI DLL is the solution.
Installing Wildcard Application Mappings (IIS 6.0) may also be useful.
Without rewriting the CMS, you can put a physical file in a new directory "/example". This will trigger ASP.NET to intercept the request, and hopefully load your page.
If you want to really hack it up you can change the IIS 404 page to be a .NET page in your application that can handle the original request and redirect to the page you really want.
Yes this is correct because /example is not pointing to any file, it is pointing to directory in the web server. Check that Default.aspx/ default.html or any other atleast one of them exists in your app.
If you are using ASP.Net MVC for REST then check your actions are properly written.

ASP.NET URL rewriting for DB query from URL content without extension

I am trying to create a very simple ASP.NET application that presents information retrieved from a database based on the URL, in a similar way to a wiki, but in this case the site is read-only. An example of a URL I'd like would be:
http://www.foo.com/bar
The application would then use "bar" as a SQL query parameter to show information from the database that matches "bar".
I've looked into many URL re-writer options for IIS6 (and this would be on a dedicated server), but I'm still not sure which one to use for this application.
To perhaps clarify, I only need to run the site from a single default.aspx file, but I want it to work as described above. The overall site logic will be very simple.
I am hoping that someone with more experience in this area can help me out -- I am looking for the simplest solution that will address this one scenario.
IIS6 only directs requests to the asp.net engine if that extension has been registered. By default the registered extensions are aspx ascx asmx etc...
If you cannot base you database query on a query string parameter (e.g. foo.com/default.aspx?query=bar) then the best you can do on IIS6 is a wildcard mapping. Basically this means that every request will be directed over to asp.net (including images scripts and styles.) obviously this will degrade performance.
To enable wildcard mapping right click on your site in IIS manager and go to Properties -> Home Directory -> Configuration -> Mappings at the bottom click insert and type in the path to the asp.net isapi dll (you can copy it from the aspx extension above) and uncheck 'Verify that file exists'.
After making the changes you'll be able to request foo.com/bar
(another method might be to make a request to foo.com/default.aspx/bar)
Just for the record, IIS URL Rewrite 2 supports this, you can install the extensibility samples that include a DB provider. Works on IIS 7+ only.
http://www.iis.net/download/urlrewrite
http://code.msdn.microsoft.com/rewriteextensibility

Why doesn't url rewrite work?

In asp.net 3.5, I'm rewriting the url
http://www.abc.com/archive/1108/harpersdecember
to the following
http://www.abc.com/article.aspx?docId=78
I'm using this code to do it:
Context.RewritePath("/article.aspx?docId=78");
It works fine locally but when I upload to the remote web server, I get a 404 when trying to reference the above page. Any suggestions why it works locally but not remotely?
You may need to create a wildcard mapping in IIS on the remote server so that all requests are processed by ASP.Net. If you do not do this any URLs without .ASPX on the end will not run through your URL rewriting code.
There is a good explanation of this (and other reasons you might use it) on Scott Guthrie's blog.
Not "may" - you definitely need to create a wildcard mapping. Visual Studio uses the cassini web server which essentially passes all requests through .net. IIS only forwards specific mapped requests (by default .aspx, .asmx, etc..) to .net - rewriting a URL in asp.net requires adding a new mapping to get the request to asp.net in the first place
Sounds to me like the production server does not have a default aspx page, ie: default.aspx. If it did, it would reroute the request to your handler.
Easy way to verify this, would be to create a directory and place a default.aspx file in it and try to request it using only the dir name, ie: server.com/newdir/
If that gives you a 404, then you know it for sure.

ASP.NET/IIS: 404 for all file types

I set up 404 handler page in web.config, but it works ONLY when extension of URL is .aspx (or other which is handled by ASP.NET).
I know I can setup static HTML page in website options, but I want to have a page.
Is there any options to assign ASPX handler page for all request extensions in IIS?
The direct question was whether or not there are options to assign the ASPX handler to all request extensions: Yes, there is. I'll discuss how to do that shortly.
First, I think the "hidden" question -- the answer you really want -- is whether or not there's a way to redirect all 404 errors for pages other than ASPX, ASMX, etc. Yes, there is, and this is the better choice if it'll solve the issue you're having.
To redirect all 404s in IIS 6, right click your web application root (whether it be its own site or a virtual directory in the main site), and choose "Properties." From there, choose the "Custom Errors" tab. Find 404 in the list and change it to the redirect you want.
Now, if that won't suffice -- and I really hope it does -- yes, you can run every page through the ASPX handler. However, doing so comes at a fairly high cost in terms of efficiency -- raw HTML/image serving is considerably faster than anything dynamic.
To do this, right click your web application root and choose "Properties." Choose the "Home Directory" tab. Click "Configuration;" a new window will pop up. Copy the path from one of the ASP.NET page serves, and then use it for a wildcard application map.
Bear in mind, again, this is the wrong answer most of the time. It will negatively impact your performance, and is the equivalent of using a chainsaw to carve a turkey. I highly recommend the first option over this one, if it will work out for you.
For information:
This is one of the several nice things that IIS7 brings - all pages are routed through the handler such that you can do custom 404s and - usefully - directory and file level security for any file (based on the same web.config stuff as for asp.net files prior to IIS7).
So notionally "use II7" is an answer (will be "the" answer in time) - but of course its not a terribly practical one if you're not hosting/being hosted on W2k8 (or higher).
The web.config can only set up errors pages for pages controlled by it's web site. If you have any other pages outside the purview of the ASP.Net application, then you set up handling for them in IIS. There's an option in there for configuring the 404 page where you can point it to your custom page.
Only other thing i can think of is passing ALL extensions to asp.net.
This way all types of files get processed by asp.net and your custom error page will work.
In the IIS application configuration, you can set a wildcard mapping (".*") to C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll
You can setup wild card mapping in IIS (Application configuration/Mappings/Wildcard mappings/ - just set aspnet_isapi.dll as executable and uncheck the Verify that file exists box) that will route all incoming requests to your app - so you can control the behavior directly from it.
You don't have to setup static page in your IIS application settings. Imho, you should be able to setup valid url (e.g. /error_handler.aspx) from your app that will be used as landing page in case of specific server error.
In IIS you can set a Custom Error for 404 errors and direct it to a URL in the site properties.
It shows a static html by default
C:\WINDOWS\help\iisHelp\common\404b.htm
You can change it to a relative url on your site.

Resources