<rewrite url="~/Blog" to="~/Blog.aspx" processing="stop"/>
This doesn't work, it only seems to work if I define a replacement filename as well... How do I make it so that
http://www.mysite.com/Blog
Goes to:
http://www.mysite.com/Blog.aspx
See Scott Guthrie's Post
With IIS 6.0 (Windows XP and Windows Server 2003), you can't do this with the usual configuration. IIS looks for a file extension to route the URL to determine which of the installed engines (classic ASP, ASP.Net, PHP, etc.) the request should get routed to. When there's no extension, IIS looks in the corresponding folder (virtual or real) for a default document, like default.aspx or index.htm, etc.
With IIS 7, you can use Integrated mode to get the behavior you want. With IIS 6, you can still do it by specially configuring it to route all URLs to ASP.Net, regardless of extension. For efficiency, you may want to refine it so that static files like images are not routed to ASP.Net. How to do this is explained here: http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/5c5ae5e0-f4f9-44b0-a743-f4c3a5ff68ec.mspx?mfr=true
Consider using MVC routing. If you're using .NET 4 it's even easier. All you have to do is reference System.Web.Routing and then in the global.asax file you can do routes.MapPageRoute("Blog Route", "Blog", "~/Blog.aspx");
Helpful references:
http://msdn.microsoft.com/en-us/library/cc668201.aspx (.net 4)
https://web.archive.org/web/20211020111718/https://www.4guysfromrolla.com/articles/012710-1.aspx (.net 4)
https://web.archive.org/web/20201205221404/https://www.4guysfromrolla.com/articles/051309-1.aspx (.net 3.5)
Related
I am trying to migrate a site from classic ASP to .Net (using WebMatrix). I am planning on mixing the new .cshmtl pages in with the legacy .asp files and slowly migrate the site over time. To date, I have individual .asp pages for each piece of content (each url). As I move to .Net, the content will be coming from a database, so there will not be specific pages for each url. I love the power of routing that is available in .Net and it works well and plays nicely with the classic ASP with one exception so far.
My example is that I have an ASP page located at /crafts/default.asp and currently reference that everywhere as just /crafts/. The IIS default document setting takes care of serving the default.asp page when i just reference /crafts/. I want to start to post new items into this area of the site but run as .Net pages. so I created a /crafts.cshtml page which is designed to handle different urls and go to the database and lookup the item and display the info. So as an example I have legacy page at /crafts/fall-crafts.asp and this works fine - because there is an .asp page sitting there named that. But when i reference /crafts/ .Net takes over and serves the /crafts.cshtml page instead of serving the /crafts/default.asp file. Is there something I can do to keep the power of .Net routing in place, but still have IIS serve the default document in this case?
Modify your RouteConfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("crafts/");
...
See RouteCollection.Ignore for documentation.
Similar question (mentions some other possibly needed workarounds): Ignore folder in ASP .NET MVC
The default "routing" built into the Web Pages framework relies on matching incoming URLs to physical files on disk, looking first for the existence of cshtml or vbhtml files. All the time you have a crafts.cshtml file at the same level as a folder called crafts, the file will be served first. The solution is to remove or rename you crafts.cshtml file, but currently that will mess up your URLs.
There is a package which offers greater management over routing in Web Pages applications. It's called Routing For WebPages. It allows you to route URLs to any file name you like. You can download that via the Package Manager or Nuget and read this article that I put together that explains how to use it: http://www.mikesdotnetting.com/Article/187/More-Flexible-Routing-For-ASP.NET-Web-Pages.
Ok I had a huge Issue giving this a proper title, my excuses for that.
Anyways I have started slowly to look at Web and ASP.NET again, I am a C# developer but I have mostly worked with Windows applications the past 5 years or so, It is not that I haven't touched the web as such in that time, but this is as web services (Restfull as well as the ugly SOAP services) I have also worked with more "raw" web requests.
But I have not worked with IIS or ASP.NET in all that time.
What I would like to do is hos a web page that uses a URL style I could best describe with "like rest", hence the "Restfull urls" title. Because I think most people thinks of such URL's in terms of:
http://example.com/item/
http://example.com/item/23/
and so forth. Not that they have to look like that, however I would like to use such URL's instead of
http://example.com/item?id=23
I know subtext does this, but i have not had any luck finding it in their code base.
Now as far as I can tell I could just implement some IHttpHandler's, but at least for the examples I have seen of that, they write the page source back in code, and I still have master pages etc. I wish to use instead of taking over all that stuff my self, I really just kinda wants to route http://example.com/item/23/ to http://example.com/item and asking for the item with id 23...
I hope this makes sense at all >.<... And that someone has some better examples at hand that what I have been able to find.
You can achieve this using Routing here is a link to an MSDN blog, The .Net Endpoint - Using Routes to Compose WCF WebHttp Services that should get you started.
If you're looking at asp.net/IIS, another option to look at is ASP.Net MVC. It's pretty straight forward to create RESTful services.
Here's a tutorial:
http://www.codeproject.com/Articles/233572/Build-truly-RESTful-API-and-website-using-same-ASP
So here are your options-
For .net 3.5 sp1 framework with IIS7 you can use asp.net routing feature to have MVC style urls that you mentioned should create a custom route handler implementing IRouteHandler interface as explained here How to: Use Routing with Web Forms and register your route rules in Application_Start method in Global.asax. For your example you can register a route like this
routes.Add("ItemRoute", new Route
(
"item/{itemId}",
new CustomRouteHandler("~/item.aspx")
));
and then you can access itemId in your routed item.aspx page by checking request context item
requestContext.HttpContext.Items["itemId"]
For .net framework 4 MVC you dont have to create a custom handler, you can directly use
routes.MapPageRoute("ItemRoute", "item/{itemId}", "~/item.aspx");
in you global.asax Application_Start method.
This link explains more about the Routing
A way of achieve this is using URL rewriting.
If you're planning to host your Web application in Internet Information Services 7.x, you can take advantage of IIS URL Rewriting Module:
http://www.iis.net/download/urlrewrite
URL rewriting is just mapping a friendly URL to an unfriendly, common one, which is programming-friendly to inspect GET parameters.
For example:
http://yourdomain.com/item/48 => http://yourdomain.com/Items.aspx?Id=48
How can I add a routing rule that takes this URL
http://localhost:57334/Blog/index.aspx?Id=23
and converts to this:
http://localhost:57334/Blog/Id/23/blog
Routing rules typically interprets the urls you give and maps them to resources meant to handle those requests. Your url suggests you are using webforms and not asp.net mvc.
What i think you need is URL rewriting... you could check out
this link
Hope this helps
It depends if you are using IIS 6 or 7
If you are using IIS6 you will need to install an add-in and you will need to install something like:
www.isapirewrite.com or www.urlrewriter.net
If you are using IIS7 take a look at http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/
I have an application written in .NET 3.5 with C# as the language. I'm using Web Forms, but using url routing with the routes defined in my global file. Everything is working as expected. In order for the pretty paths (see: user/665 instead of user.aspx?uid=665) to work properly, I had to add a wildcard mapping in IIS5.1 (local box, not test, staging, or production) the aspnet_isapi file for the 2.0 framework. Everything works fine.
Now, my site needs a plugin for PHP. However, the PHP files are now being serviced by ASP.NET due to the wild card mapping, and hence are not processed by the PHP interpretter. Is there any way to get around this? Would I have to add some sort of handler to my web app that will take all PHP requests being handled by the ASP.NET framework and have them routed to the PHP engine? Is there an easier way? Maybe a way to exclude them in the web.config (PHP files) and have them served by the proper PHP engine?
Thanks all!
-Steve
This is a solution, but is not an elegant way (IMHO):
Create a virtual directory
Have it point to the folder with the files (in this case, a PHP plugin)
Give it the proper permissions
Change the config options for the virtual directory in IIS and make sure the wildcard mapping for that directory is removed.
This works like a charm for my situation. However, is there any way to not have to deal with virtual directories?
The problem is that the PHP extension needs to be registered.
In IIS Manager right-click on Default Website -> Properties -> Home Directory -> Configuration
Under Application Mappings make sure that .php is added and is it pointing to PHP.EXE. There should be an entry like this: extension .php, executable path C:\PHP\PHP.EXE %s %s
From what I gather, the problem is that ASP.NET is attempting to route your PHP requests, so what I would do is add a StopRoutingHandler() to your routes in the global.asax. Something like this should work:
routes.Add(new Route("{resource}.php/{*pathInfo}", new StopRoutingHandler()));
Edit: Be mindful that routes are processed in order, so I would add this to the top of your routes.
I have a custom site I'm building with automatic url rewriting using a custom engine. The rewriting works fine as long as the page url doesn't end in somehting like .htm or .html. For these pages it goes directly to the iis 404 page instead of hitting my rewriting engine first.
I have the * wildcard handler in the "Home Directory" section of the IIS6 configuration of that website but these urls seem to be ignored by it (altho things like css, jpg, js, etc to get sent to the url handler in my web project). How do i set up IIS6 to force these urls to get sent to the handler, while still serving the page if it exists normally?
The handler basically does this
if (!File.Exists(Request.Path))
{
doMyRewriting();
}
I have to assume that using a block like this (just and example, the real one does some other stuff to format the Request.Path to be proper with everything) should run the "doMyRewriting()" if the requested file does not exist otherwise it will serve the page normally. Am I mistaken?
If I tell IIS specifically to send .htm and .html pages thru to the .NET handler the rewriting works but if the page is actually there it will not serve it.
Any help would be greatly appreciated.
Thanks in advance!
Don't know if you can or would want to do this, but there is the Ionics Isapi url rewriter you can use.
http://www.codeplex.com/IIRF
Basically install that then set a rule to remove the .html that way it hits your rewrite engine. I use it on IIS 6 with several of my blogs.
I think if you are having IIS send all requests to .NET and your handler, then your handler will need to detect if the page exists and serve it instead of rewriting.
UrlRewriting.NET has an option to do this - you might want to look at their code to see how they're handling this case.
In my opinion, rewriting URLs with IIS 6 is best handled with an ISAPI filter written as unmanaged native code. Otherwise, you run into the issues you've mentioned - having to map all extensions to ASP.Net and losing the ability for simple file handling. With an ISAPI filter, you can choose to not rewrite some URLs and let IIS handle them as normal.
To get started, I suggest reading the ISAPI Filter Overview on MSDN.
If your filter absolutely needs the .Net framework runtime, it is possible to write a small ISAPI filter shell that hosts the CLR and forwards the requests to some managed code. The Filter.Net Framework takes this approach and may be suitable for your needs. There is the small drawback to this approach in that you will have to use the same .Net version as any ASP.Net applications that are run in the main IIS process.