Can I redirect a web service (ASMX) URL to an ASP.NET MVC site?

I saw how you can add custom routes to WebForms using this code.

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)); } } 

I am trying to get a similar job, but for web service files (TestService.asmx.) This previous method throws an exception because the page does not inherit from IHttpHandler. I saw other code that uses WebServiceHandlerFactory like this

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

This returns an IHttpHandler as I need, but needs to pass an HttpContext to it, but the only thing I have is also part of RequestContext - this is HttpContextBase. From what I can say, I cannot convert it to an HttpContext.

Any ideas? Or maybe in a different way? What I'm trying to accomplish is to control the URLs for my web services through a regular routing system. For example, you want TestService.asmx to appear as ExampleTestService /.

+4
source share
2 answers

Interesting idea. I did not know that you could use web forms in this way. We are currently integrating legacy web form applications with IgnoreRoutes . I will definitely add your question;)

But maybe I can help you with your problem. The good old HttpContext still exists, it just wrapped MVC in a friendly HttpContextBase .

You can get the original HttpContext with

 var context = System.Web.HttpContext.Current; 

In the controller, you must specify the type completely in order to distinguish it from the HttpContext controller HttpContext

+1
source

Here is how I do it:

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

Source: https://habr.com/ru/post/1286500/


All Articles