简体   繁体   中英

How to use this WebService in MVC ASP.NET

I want to use WebService ( *.asmx ) in MVC ASP.NET. I created this class:

public class ServiceRouter : IRouteHandler
{
    private readonly string _virtualPath;
    private readonly WebServiceHandlerFactory _handlerFactory = new WebServiceHandlerFactory();

    public ServiceRouter(string virtualPath)
    {
        if (virtualPath == null)
            throw new ArgumentNullException("virtualPath");
        if (!virtualPath.StartsWith("~/"))
            throw new ArgumentException("Virtual path must start with ~/", "virtualPath");
        _virtualPath = virtualPath;
    }
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return _handlerFactory.GetHandler(HttpContext.Current, requestContext.HttpContext.Request.HttpMethod, _virtualPath, requestContext.HttpContext.Server.MapPath(_virtualPath));
    }
}

Then, in RouterConfig.cs , I added:

routes.Add("RouteName", new Route("Service", 
            new RouteValueDictionary() {{ "controller", null }, { "action", null }}, 
            new ServiceRouter("~/WebService1.asmx")));

I can access the web service, but I get an error message when clicked by a method invoke any:

Server Error in '/' Application.

The resource cannot be found.

How do I fix this?

**

After studying, I was able to use the webservice in ASP.NET MVC.

** This is my way. In RouterConfig.cs in just add:

routes.IgnoreRoute("{resource}.asmx/{*pathInfo}");

According to Scott Hanselman, the request is by default not handled by the ASP.NET MVC routing mechanism:

Why doesn't ASP.NET MVC grab the request? Two reasons. First, there's an option on RouteCollection called RouteExistingFiles. It's set to false by default which causes ASP.NET MVC to automatically skip routing when a file exists on disk. ( Source )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM