IgnoreRoute with webservice - 从路由中排除 asmx URL
我将 filevistacontrol 添加到我的 asp.net MVC Web 应用程序中。
我有一个在路由中被忽略的 media.aspx 页面,
routes.IgnoreRoute("media.aspx");
它成功运行并提供标准的 Web 表单页面。
添加 filevistacontrol 后,我似乎无法忽略该控件对其 Web 服务进行的任何调用。
例如,以下ignoreRoute似乎仍然被MvcHandler拾取。
routes.IgnoreRoute("FileVistaControl/filevista.asmx/GetLanguageFile/");
抛出的异常是:
'The RouteData must contain an item named 'controller' with a non-empty string value'
提前致谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
简短回答:
详细回答:
如果您的服务可以位于路径的任何级别,这些选项都不适用于所有可能的 .asmx 服务:
默认情况下,路由模式中的参数将匹配,直到他们找到了一条斜线。
如果参数以星号
*
开头(如这些答案中的pathInfo
),它将匹配所有内容,包括斜杠。因此:
.asmx
服务,因为{resource}
不会匹配斜杠。 (适用于诸如http://example.com/weather.asmx/forecast
之类的东西).asmx
服务相距一级的服务root.{directory}
将匹配路径的第一段,{resource}
将匹配服务的名称。 (适用于诸如http://example.com/services/weather.asmx/forecast
之类的东西)没有一个适用于
http://example.com/services/weather/weather。 asmx/forecast
)解决方案是使用
IgnoreRoute
方法的另一个重载,该方法允许指定约束。使用此解决方案,您可以使用匹配所有 url 的简单模式,如下所示:{*url}
。然后您只需设置一个约束来检查此 url 是否引用.asmx
服务。此约束可以用如下正则表达式表示:.*\.asmx(/.*)?
。此正则表达式匹配以.asmx
结尾的任何字符串,可选地后跟斜杠及其后的任意数量的字符。所以,最终的答案是这样的:
Short answer:
Long answer:
If your service can be in any level of a path, none of these options will work for all possible .asmx services:
By default, the parameters in a route pattern will match until they find a slash.
If the parameter starts with a star
*
, likepathInfo
in those answers, it will match everything, including slashes.So:
.asmx
services in the root path, becasuse{resource}
will not match slashes. (Would work for something likehttp://example.com/weather.asmx/forecast
).asmx
services which are one level away from the root.{directory}
will match the first segment of the path, and{resource}
the name of the service. (Would work for something likehttp://example.com/services/weather.asmx/forecast
)None would work for
http://example.com/services/weather/weather.asmx/forecast
)The solution is using another overload of the
IgnoreRoute
method which allows to specify constraints. Using this solution you can use a simple pattern which matches all the url, like this:{*url}
. Then you only have to set a constraint which checks that this url refers to a.asmx
service. This constraint can be expressed with a regex like this:.*\.asmx(/.*)?
. This regex matches any string which ends with.asmx
optionally followed by an slash and any number of characters after it.So, the final answer is this:
我使用这个(其他答案的组合)让它工作:
I got it to work using this (a combo of other answers):
使用时会发生什么:
如果这不起作用,请尝试使用 ASP.NET 路由调试器来帮助您:
http://haacked.com/archive/2008/03/13 /url-routing-debugger.aspx
What happens when you use:
If that doesn't work, try using the ASP.NET Routing Debugger to help you:
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
试试这个:
这是基于 Phil Haack 的建议 此处。
Try this:
This is based on a Phil Haack recommendation stated here.
你有没有尝试过:
Have you tried:
如果您发布路由配置的源代码,将会有所帮助。我将在黑暗中进行尝试并确保您的 IgnoreRoute() 调用全部位于路由定义的顶部。
IgnoreRoute 的工作方式是创建一个与忽略的路由 URL 和约束相匹配的路由,并附加一个
StopRoutingHandler
作为 RouteHandler。 UrlRoutingModule 知道 StopRoutingHandler 意味着它不应该路由请求。众所周知,路由按照定义的顺序进行匹配。因此,如果您的
{controller}/{action}/{id}
路由出现在"FileVistaControl/filevista.asmx/GetLanguageFile/"
路由之前,那么它将与“{controller}/{action}/{id}”
路线。我可能完全没有根据,但如果不看到你的消息来源就很难知道。希望这有帮助。并贴出源码!你会得到更好的答案。
It would help if you posted the source for your route configuration. I'm going to take a shot in the dark and say to make sure that your IgnoreRoute() calls are all at the top of your routing definition.
The way IgnoreRoute works is to create a route that matches the ignored route URL and constraints, and attaches a
StopRoutingHandler
as the RouteHandler. The UrlRoutingModule knows that a StopRoutingHandler means it shouldn't route the request.As we know, the routes are matched in the order of which they are defined. So, if your
{controller}/{action}/{id}
route appears before your"FileVistaControl/filevista.asmx/GetLanguageFile/"
route, then it will match the"{controller}/{action}/{id}"
route.I may be totally off base here, but it's hard to know without seeing your source. Hope this helps. And post source code! You'll get better answers.