2009-06-20 53 views
4

我已經看到如何使用這樣的代碼將自定義路由添加到WebForms。我可以在ASP.NET MVC站點中路由Web服務(ASMX)的URL嗎?

public class WebFormsRouteHandler : IRouteHandler 
{ 
    public string VirtualPath { get; set; } 

    public IHttpHandler GetHttpHandler(RequestContext requestContext) 
    { 
     // Compiles ASPX (if needed) and instantiates the web form 
     return (IHttpHandler) BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof (IHttpHandler)); 
    } 
} 

我試圖得到類似的東西的工作,但對於Web服務的文件,因爲該頁面不從的IHttpHandler繼承(TestService.asmx),以前的方法拋出異常。我已經看到了使用WebServiceHandlerFactory這樣

return new WebServiceHandlerFactory().GetHandler(context, requestType, url, pathTranslated); 

,像我需要返回IHttpHandler的一些其他的代碼,但它需要通過一個HttpContext的,但我有機會爲過的RequestContext的一部分的唯一的事情是HttpContextBase。從我可以告訴我不能轉換爲HttpContext。

任何想法?或者,也許有另一種方式去解決它?我試圖完成的是通過正常的路由系統來控制我的Web服務的URL。一個例子是想讓TestService.asmx成爲ExampleTestService /。

回答

1

有趣的想法。我不知道你可以用這種方式使用Web表單。我們目前正在將舊的網頁表單應用程序與IgnoreRoutes集成。我肯定會收藏你的問題;)

但也許我可以幫助你解決你的問題。好的舊HttpContext仍然存在,它只是由MVC包裝成模擬友好HttpContextBase

您可以找回原來的HttpContext

var context = System.Web.HttpContext.Current; 

在控制器,你必須完全指定類型從所謂的控制器屬性區分HttpContext

+0

我得到了部分成功。我可以得到我的自定義URL來調出Web服務,但嘗試調用它時會失敗。 – 2009-06-25 05:15:14

+0

然後你最好的機會是通過ASP.NET代碼進行調試,如果失敗,找出原因...... – chris166 2009-06-25 05:46:35

1

這是我要做的事:

Return New WebServiceHandlerFactory().GetHandler(HttpContext.Current, "*", "/Build/WebService.asmx", HttpContext.Current.Server.MapPath(aspxToLoad))