【问题标题】:How to use this WebService in MVC ASP.NET如何在 MVC ASP.NET 中使用此 WebService
【发布时间】:2016-04-19 01:46:04
【问题描述】:

我想在 MVC ASP.NET 中使用 WebService (*.asmx)。 我创建了这个类:

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

然后,在RouterConfig.cs,我补充说:

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

我可以访问网络服务,但是当我被方法调用时收到错误消息:

“/”应用程序中的服务器错误。

找不到资源。

我该如何解决这个问题?

**

学习后,我可以在ASP.NET MVC中使用webservice了。

** 这是我的方式。在RouterConfig.cs中只需添加:

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

【问题讨论】:

  • 你把 WebService1 放在应用程序的根目录了吗?
  • 是的。 WebService1.asmx 和 startup.cs 同级!
  • 我可以访问 localhost/Webservice1.asmx 和 localhost/Webservice1.asmx?op=helloworld 但我无法访问 localhost/webservice1.asmx/helloworld。 Helloworld 是 webservice1.asmx 的方法!

标签: c# asp.net asp.net-mvc web-services asp.net-mvc-4


【解决方案1】:

根据 Scott Hanselman 的说法,该请求默认情况下不由 ASP.NET MVC 路由机制处理:

为什么 ASP.NET MVC 不抓取请求?两个原因。首先,RouteCollection 上有一个名为 RouteExistingFiles 的选项。它默认设置为 false,这会导致 ASP.NET MVC 在磁盘上存在文件时自动跳过路由。 (Source)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多