Restfull urls for ASP.NET page on IIS

320 views Asked by At

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.

4

There are 4 answers

0
DotNetUser On BEST ANSWER

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

4
Lloyd On

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.

0
Matías Fidemraizer On

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:

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
0
bryanmac On

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