在.net中实现restful url,其中使用IHTTPHandler实现服务

发布于 2024-10-12 11:14:34 字数 403 浏览 5 评论 0原文

我想在 ASP.NET 中实现一个宁静的服务。我希望它与 .Net 2.0 和 IIS 5+ 兼容。我被迫不使用 ASP.NET MVC 或 REST 入门工具包。通过阅读互联网,我了解到它可以使用 HTTPHandlers 来实现。问题是,请求将以 POST 请求的形式传入。我希望 URL 类似于:

http://abc.com/MyService/MyMethod1/http://abc.com/MyService/MyMethod2/

有解决方法吗?

谢谢, 瓦米普

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

死开点丶别碍眼 2024-10-19 11:14:34

最好的选择是使用 URL 重写。这在 IIS5 中并非易事。我所知道的方法如下:

方法 1 - ISAPI 过滤器

这些是低级模块,允许您操作传入的请求。其中之一的编程非常复杂且难以调试。如果您选择这条路线,最好使用已经构建的版本,例如 ISAPI_Rewrite

方法 2 - IHttpModule

这些是托管的 ASP.Net 模块,可以轻松地在应用程序中添加/删除。同样,您最好使用预构建的组件,例如 UrlRewriter.NET。使用其中之一的问题(如 BrainLy 提到的)是,您必须配置 IIS 5 以将所有传入请求映射到 ASP.Net,如下所示 (链接):

  1. 打开 IIS 并导航至“主目录选项卡”
  2. 选择“配置”
  3. 单击“添加”并输入“C:\ WINNT\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll”在可执行文件框中。对于文件扩展名,输入“.*”。最后,确保未选中“检查文件是否存在”。

需要注意的一件有趣的事情是 ASP.Net 本身就是一个 ISAPI 模块:)

一旦您能够使用这些工具之一操作 URL,您就可以轻松地重写由 default.aspx 页面(或任何处理程序)处理的 RESTful url您选择使用)。

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):

  1. Open Up IIS and Navigate to the “Home Directory Tab”
  2. Select “Configuration”
  3. 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).

鸠书 2024-10-19 11:14:34

如果您可以仅允许限制 IIS 7.0 及更高版本,则可以使用 URL 重写 http://www.iis。 net/download/URLRewrite 可以很轻松地做到这一点。

请问为什么需要支持IIS 5+?这是一项已有 11 年历史的技术,希望人们能够放弃这些平台,转而使用更新的版本。另请记住,对其中一些平台的支持很快就会结束。

如果担心的是运行 Windows XP 的开发人员,我会指出 IIS Express 包含 7.5+ 版本的功能,并且适用于 Windows XP 及更高版本的所有平台。

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.

对岸观火 2024-10-19 11:14:34

我认为这很难做到,因为如果没有在 IIS 中进行一些额外的配置,IIS 5 不会让您处理非 ASP.NET 文件扩展名。这意味着您仅限于以 .aspx 等结尾的 URL。要处理示例中的 URL,您需要映射 ASP.NET 来处理 IIS 中的所有 URL,实现某种类型的 URL 重写,或引入某种 hacky 404 重定向。

一旦你有了正确的映射,你就可以连接一个 IHttpHandler,但是你必须自己解析传入的请求,以确定哪个是 /MyService/MyMethod1/ ,哪个是 /MyService/MyMethod2/ 。如果您的方法很简单,那么使用正则表达式很容易做到这一点。

您应该从一个简单的处理程序开始,例如 this一。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文