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/
Related
Is there are any way to remove # in URL of .NET Core project? My project is using app.UseFileServer() to serve react application (production/build from react app) inside wwwroot. It is working properly if only we acess the root URL of .NET Core that serves using IIS, for example:
https://yourdomain.com
But if we want to go to the route of react for example route about, it needs a URL like this:
https://yourdomain.com/#/about
I don't know why the # sign should be there, is there are any way to make the URL like this:
https://yourdomain.com/about
so it directly accesses the route about of react?
You are using hashHistory routing in react app. In the case if you want to remove # please use browserHistory routing
for more detail check this
https://stackoverflow.com/a/35185338/7327166
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
<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)
I want to make url rewrite by httpd.ini in ASP.NET 3.5.
I want to rewrite www.mydomain.com/random_name to www.mydomain.com/PageDetails.aspx?name=random_name
How do it in httpd.ini?
It sounds like your host is using Helicon ISAPI rewrite module with IIS6
If that is the case, confirm with them first, then you can use Apache mod_rewrite style rules. For example, I think, this rule will work for you:
RewriteRule ^/([^/\.]+)/?$ PageDetails.aspx?name=$1 [L]
It has been a while since I have done this so it might not be spot on. Hopefully it will be enough to get you on the right track.
My server hosting uses url only rewrite by httpd.ini, not http module etc.
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.