Implement restful url in .net where service is implemented using IHTTPHandler - c#

I want to implement a restful service in ASP.NET. I want it to be compatible with .Net 2.0 and IIS 5+. I am constrained to not use ASP.NET MVC or REST starter kit. By reading on internet I have learned that it can be implemented using HTTPHandlers. The problem is, the request will come in as a POST request. And I want to URL to be like:
http://abc.com/MyService/MyMethod1/
and
http://abc.com/MyService/MyMethod2/
Any workarounds for this?
Thanks,
Vamyip

Your best option is to use URL Rewriting. This is non-trivial in IIS5. The methods I know of are as follows:
Method 1 - ISAPI filter
These are low-level modules that allow you to manipulate the incoming request. Programming one of these is hairy and tough to debug. If you go this route, you are better off using one that has already been built like ISAPI_Rewrite.
Method 2 - IHttpModule
These are managed ASP.Net modules that are easy to add/remove from your application. Again, you are better off using a pre-built component like UrlRewriter.NET. The issue with using one of these, (as BrainLy mentions), is that you have to configure IIS 5 to map all incoming requests to ASP.Net as follows (link):
Open Up IIS and Navigate to the “Home Directory Tab”
Select “Configuration”
Click “Add” and enter “C:\WINNT\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll” in the Executable box. For the file extension, enter “.*”. Finally, make sure that “Check that file exists” is not checked.
One interesting thing to note is that ASP.Net is itself an ISAPI module :)
Once you are able to manipulate URLs using one of these tools, you can easily rewrite the RESTful urls to be handled by your default.aspx page (or whatever handler you choose to use).

If you can allow the restriction of only IIS 7.0 and above you could use URL Rewrite http://www.iis.net/download/URLRewrite to do that pretty easily.
Can I ask why is it that you need to support IIS 5+? That is an 11 year old technology that hopefully people will move out of those platforms in favor of more recent versions. Also keep in mind support for some of those platforms is ending pretty soon.
If the concern is developers running Windows XP I would point out that IIS Express includes version 7.5+ functionality and is available for all platforms Windows XP and above.

I think this will be difficult to do because IIS 5 will not let you handle non ASP.NET file extensions without some additional configuration in IIS. That means you are limited to URLs ending in .aspx etc. To handle URLs like those in your examples you need to map ASP.NET to handle all URLs in IIS, implement some type of URL rewriting, or introduce some kind of hacky 404 redirection.
Once you have the correct mapping in place you can wire up an IHttpHandler, but you will have to parse the incoming request yourself to work out which is /MyService/MyMethod1/ and which is for /MyService/MyMethod2/. If your methods are simple then it is easy to do this with a regular expression.
You should start with a simple handler like this one.

Related

Using (and enforcing) HTTPS in an APS.NET Core 2 Application

I was doing research for implementing (and enforcing) HTTPS on an ASP.NET Core 2 application, and I saw Enforcing HTTPS in an ASP.NET Core app, but it got me confused. It mentions two methods: using RequireHttpsAttribute or using a URL rewrite, but in the warning at the beginning of the page, they mention that do NOT use RequireHttpsAttribute because it's doing a redirect, but isn't it what the URL rewrite is also doing? So, are we able to use any of them for Web APIs even if they receive "sensitive information" (as mentioned in the warning in the docs page).
Also, I have heard about HSTS, and I did some research, but it doesn't seem to me that there's a standard implementation for it in ASP.NET Core. I stumbled upon a few NuGet packages, but I couldn't find one from a more "official" provider.
Can you help me understand this please?
Your confusion may lie in that the goal of the article is about a non-WebAPI application. Thus the warning - do not apply to WebAPI's. Do not use either of the methods for WebAPI's.
Users that connect to an application on http://example.com (by typing the url in a browser) should be redirected to https://example.com. This may be done using one of the two methods.
Applications that connect to http://webapi.example.com (like a web application) should be NOT be redirected to https://webapi.example.com. Port 80 should either be closed or return status 400 (for nginx and IIS, change the status codes in these answers to 400).
Users cannot be burdened with technical security details. Developers, however, should be fully aware and can do the connection directly to https without any redirect what so ever.
Considering your second remark about "official" support for HSTS, take a look at this sample from the asp.net core docs. To be fair, this was only committed a few months ago (as part of 2.1 milestone) as of this writing, not a lot of documentation yet.

Is there such a thing as an entirely empty website project?

I'm looking at working my long-standing API to run on IIS rather than in a desktop app as it is now. Everything on the API is working so I'd rather not change too much if I don't have to. I know about the new Web API template in ASP.NET MVC 4 and I've worked with it, but I found that it didn't give me the control over everything that this particular project needs.
So my question is, is there any way to build an application for IIS that has something like an entry-point where I can just get a web request then use entirely my own code from there? Or do I have to build something that uses the Web API?
Yes, you'll want an ASP.NET handler.
How To Create an ASP.NET HTTP Handler by Using Visual C# .NET
http://support.microsoft.com/kb/308001
You'll need to handle parsing the request and serializing the result yourself. It's probably much better to create a web-api facade in front of your services than trying to do it manually.

Direction needed for self hosted web server for html, json and websockets

Background
I have a windows console app written in C# which needs UI. I started using WPF, but as I come from a web background, I want to use html, and some features of html5, including web sockets for real time communication with another application.
Initially, I'll use a web browser as the UI, though I may later host a web browser in the main app. This bit doesn't concern me at the moment.
After a lot of googling/reading, I'm going round in circles. It looks like WCF can be used to serve html, json based web services, and possibly web socket streams.
A lot of googled info relates to pre .NET 4.0 community projects. Even post 4.0 there are several NuGet packages which seem to me to overlap what is already in the framework. To a WCF noob, it's all a minefield.
So, what areas of .NET 4.0 WCF and the various open source projects should I be concentrating my efforts on.
Requirements
I require a lightweight self hosted web server. It cannot be IIS based, as users will not have it installed. The server (or servers) must:
Be able to server complete web pages, including html, linked images, css and js files. C# MiniHttpd does the job well, but is not based on http.sys. HttpListener seems to be the core of what I want, but I haven't found a complete web server project based on it.
[optionally] Be able to parse those pages through asp.net or razor
Be able to respond to web service call via json. This bit I have a working example using System.ServiceModel. Is this the right way to go?
Be able to work with the emerging Web Sockets standard. SuperWebSocket is actively developed, but doesn't appear to be http.sys or wcf based.
Preferences
I would prefer to stick to one basic stack for all 3 of my main requirements - and I suspect WCF may be that platform.
I would prefer an http.sys based approach for all three requirements, so I can reserve the relevant url/port/namespace combinations and prevent conflicts with other web servers or services
Although other SO questions may help with individual aspects of my requirements, I need advice on a more holistic approach.
Ok, answering my own question feels wrong, but...
I have since found a great CodeProject article that provides an easy to use self hosted web server for serving the html, css, js and images, and serves the json requests.
Developing Web 2.0 User Interface for Self Hosted WCF Services using HTML5, CSS3 and JQuery
I still have to settle on a WebSockets solution, but the above project is as close to what I need as is possible at the moment. It doesn't support rendering asp.net or razor, but these were my lowest priorities, as I'm happy to use only pure html and javascript for the front end.
I'll try to post more specific questions in the future :)
Except for your WebSocket support requirement, the OpenRasta framework currently supports what you describe. It definitely can run in http.sys and can be used for both creating REST services and as a web app platform. It also supports Razor and other view engines. The link page has a good comparison chart toward the middle that compares it with ASP.NET MVC & WCF.
In March 2014, a solution which fits my original requirements is ASP.NET Web Api. It can be self hosted, and can apparently be set up to serve html etc as well.
Is it possible to serve a web page from a self hosted web API in a windows service?

Integrate Delphi ISAPI DLL into an ASP.NET web application

We have all the code in Delphi and it is hard to create a new ASMX / SVC File directly in .NET because it uses encryption and weird stuff :) as it would take around 2 weeks to convert and to test...
Because of that we agreed that will keep the Delphi code and find a way to communicate between the ASP.NET application and this Delphi code, so we generated a Delphi WebService that added to IIS is an ISAPI DLL.
My first question was:
Do I really need to set up IIS and install this WebService alone, or can I use it as a part of my project (just like an ASMX file) using any special trick?
and my 2nd question, it is been hard for me to provide the fellow information on how to convert pascal into .NET so we could, using the pascal code, output an ASMX for example... I can't find anything to do this.
We have Delphi Studio 2009 and it mention in several documents that we can do .NET (how?) and there is Delphi for .NET (are we talking about and only Delphi Prism here?).
Thank you guys!
New question that will resolve my 2 questions
How can I generate an ASMX (.NET Web Service) or SVC (.NET WCF Service) from a Delphi code?
I'm reading about Delphi Prism but I can't still figure it out.
If you have an ISAPI DLL then it is installed separately from the ASMX or other ASPNET artifacts. The ISAPI DLL is installed within the IIS Manager. It's either a Filter or Extension, and it's installed specially for each.
If it's an extension, then it is probably a thing that responds to http requests. If this is true, then it will handle a set of URLs that end with a particular suffix. For example, an ISAPI extension might be registered for all requests that end in ".foo", so if you tickle http://server/pagename.foo, the request will be handled by the extension. IIS7 calls these things "handlers".
If this Delphi thing is a filter, then it is probably a thing that injects itself into each http request, regardless of which application (ASPNET, something else) eventually handles the request, and regardless of the extension on the request. A filter can do authorization checks, authentication, URL rewriting, that sort of thing. And the way you would interact with a filter is via server variables, or ... in some cases like a rewrite filter, the action of the filter is completely transparent to the "page" logic. you can ignore the fact that a filter is present.
With an authorization filter, sometimes there are authorization cookes set on the request, and you'd get that via a server variable.

IIS7 Add New Website / Host Header ASP.NET Web Form

How can I either create a new website or add a host header to an existing IIS 7 server from code?
I have looked and have not been able to find a working example?
One solution would be to create a custom HttpModule that does the work for you, however it does require you to be have a DNS that supports wildcards (*). If your DNS does not support that, you might look into managing your own DNS.
That said, here's a good post on creating an HttpModule that parses the "subdomain" passed in and forwards the traffic to the appropriate spot. He's using a search mechanism [to locate content with keywords matching the subdomain], but it can be modified for you own needs.
http://codebetter.com/blogs/brendan.tompkins/archive/2006/06/27/146875.aspx
[EDIT]
Another solution would be to find a DNS provider that offers a programmable DNS service, perhaps through a web service. You would then programatically add a subdomain to that DNS when needed from your application. That's a super simplified explanation, and doesn't take into account your business needs. Personally I prefer the HttpModule option for adding subdomains within an application as it requires less modification of the server(s) involved.
What version of .Net are you using?
If you are using .Net 3.0 or 3.5, and if you only need to configure IIS7 (not 6 or 5), check out the Microsoft.Web.Administration namespace -- it should have everything you need.
If you are using an older version of .net, have a look at WMI.
I don't have any WMI code for IIS 7 (we have a setup for an intranet application, but it uses IIS 6-compatible WMI). But, here's a link to a tool you can use for figuring out the WMI stuff: http://www.microsoft.com/downloads/details.aspx?familyid=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=en It will actually generate c# (or vb.net) code for manipulating WMI. For IIS 7, I think that the root WMI namespace is root\WebAdministration.
Also, have a look at this link, it might help Get to Know the IIS 7.0 WMI Provider Using CIM Studio

Categories

Resources