【发布时间】: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