【问题标题】:How to define endpoint without web.config or httpModule?如何在没有 web.config 或 httpModule 的情况下定义端点?
【发布时间】:2009-05-11 19:46:53
【问题描述】:

我想制作一个 HTTPhandlers 的 RESTful 应用程序,而不必通过在 web.config 中创建一个条目来定义每个端点,我想要将属性附加到类构造函数的样式,例如:

public class obj : IHttpHandler
{
  [WebGet(UriTemplate = "/accounts/{id}")]
  public obj(string id)
  {
     // this is just an eg, it worild normally include caching and 
     // a template system
     String html = File.ReadAllText("/accounts/accounts.htm");
     html.replace("id", id);
     httpcontext.current.response.write(html)
  }
}

而不是

<httpHandlers>
      <clear />
      <add verb="GET" path="/accounts/*" type="MyApp.obj" />
</httphandlers>

我现在的做法是,我在 web.config 中有 100 个端点 :( 我宁愿在课堂上定义它们。而且我也不想制作额外的文件 (.asmx)。我'想要一个只有 .htm 文件的应用程序,带有令牌和 .cs 文件

谢谢!

【问题讨论】:

  • 考虑修改代码 sn-p: "public call obj"? “公共 obj(字符串 id)”?
  • 我正在考虑修改问题:哪个方法负责解析请求 URL 并选择要实例化的 httphandler。

标签: c# .net wcf rest


【解决方案1】:

您可以使用自定义 ServiceHost 自动注册端点等,它会覆盖 ApplyConfiguration() 方法,然后虚拟化配置,使其不必位于 web.config 文件中。

Here's a starting point。它并不完全符合您的要求,但它说明了虚拟化配置的概念。

【讨论】:

  • 这看起来像要走的路,ServiceHostBase.AddServiceEndpoint Method (String, Binding, Uri) 谢谢!
猜你喜欢
  • 1970-01-01
  • 2011-06-09
  • 1970-01-01
  • 2015-11-17
  • 2023-02-22
  • 2014-09-01
  • 2021-03-29
  • 1970-01-01
相关资源
最近更新 更多