[英]Asp.Net 3.5 Routing to Webservice?
我一直在寻找一种方式来路由http://www.example.com/WebService.asmx到http://www.example.com/service/仅使用ASP.NET 3.5路由架构,而无需配置IIS服务器。
到目前为止,我已经完成了大多数教程告诉我的内容,添加了对路由程序集的引用,在web.config中配置了东西,将其添加到Global.asax :
protected void Application_Start(object sender, EventArgs e)
{
RouteCollection routes = RouteTable.Routes;
routes.Add(
"WebService",
new Route("service/{*Action}", new WebServiceRouteHandler())
);
}
...创建了这个类:
public class WebServiceRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
// What now?
}
}
......问题就在那里, 我不知道该怎么做 。 我读过的教程和指南使用的是页面路由,而不是web服务。 这甚至可能吗?
Ps :路由处理程序正在工作,我可以访问/ service /并抛出我在GetHttpHandler方法中留下的NotImplementedException 。
我想我会根据Markives为我提供的答案提供更详细的解决方案来解决这个问题。
首先,这里是路由处理程序类,它将虚拟目录作为构造函数参数传递给WebService。
public class WebServiceRouteHandler : IRouteHandler
{
private string _VirtualPath;
public WebServiceRouteHandler(string virtualPath)
{
_VirtualPath = virtualPath;
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return new WebServiceHandlerFactory().GetHandler(HttpContext.Current,
"*",
_VirtualPath,
HttpContext.Current.Server.MapPath(_VirtualPath));
}
}
以及Global.asax的routey位中此类的实际用法
routes.Add("SOAP",
new Route("soap", new WebServiceRouteHandler("~/Services/SoapQuery.asmx")));
这适用于任何想要执行上述操作的人。 我发现找到这些信息非常困难。
在GetHttpHandler(byVal requestContext as RequestContext) as IHttpHandler Implements IRouteHandler.GetHttpHandler
方法(我上面的版本)
这是Webforms 3.5的方式(我的VB)。
您无法使用通常的BuildManager.CreateInstanceFromVirtualPath()方法来调用您的Web服务,该服务仅用于实现i.httpHandler的内容,而.asmx则不然。 相反,你需要:
Return New WebServiceHandlerFactory().GetHandler(
HttpContext.Current, "*", "/VirtualPathTo/myWebService.asmx",
HttpContext.Current.Server.MapPath("/VirtualPathTo/MyWebService.aspx"))
MSDN文档说第3个参数应该是RawURL,传递HttpContext.Current.Request.RawURL不起作用,但是将虚拟路径传递给.asmx文件却很有效。
我使用此功能,以便任何网站(甚至是虚拟目录)都可以调用我的web服务,这些网站指向(在IIS中)我的应用程序可以使用类似“ http:// url / virtualdirectory / ”之类的东西调用应用程序Web服务anythingelse / WebService “并且路由将始终将其路由到我的.asmx文件。
您需要返回一个实现IHttpHandler的对象,它负责处理您的请求。
您可以查看有关如何使用该界面实现Web服务的文章: http : //mikehadlow.blogspot.com/2007/03/writing-raw-web-service-using.html
但这可能更接近你想要的http://forums.asp.net/p/1013552/1357951.aspx (有一个链接,但它需要注册,所以我没有测试)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.