在WCF中使用REST时,如何去掉URL中的.svc后缀
以Windows 7里面的IIS7.5为例。
假设我们的service method样子为
<WebGet(UriTemplate:="{parameter}")> _
<OperationContract()> _
Function ServiceMethod(ByVal parameter As String) As Boolean
<OperationContract()> _
Function ServiceMethod(ByVal parameter As String) As Boolean
我们希望将一个这样的url
http://localhost/SomeService/ServiceMethod.svc/Parameter
使用
http://localhost/SomeService/ServiceProvider/Parameter
来访问
首先从这里安装一个URLRewrite。
安装后打开IIS,从Features View里面可以看到URL Rewrite设置图标。
双击进入设置,选择Add Rule(s)...然后选择Blank Rule
进行如下设置
Fill out the form with the following data (we will describe the patterns in detail in a moment):
- Name: RemoveSvcExtension (or any name you want)
- Requested URL: Matches the pattern
- Using: Regular Expressions
- Pattern: ^([0-9a-zA-Z\-]+)/([0-9a-zA-Z\-\.\/\(\)]+)
- Ignore Case: Checked
- Action Type: Rewrite
- Rewrite URL: {R:1}.svc/{R:2}
- Append QueryString Checked
稍微解释一下Regex Pattern的含义。
- The "^" indicates the beginning of the line or string
- The parenthesis "()" allow you to define a numbered capture group, meaning that you can reference everything inside the parens later by index. In our example, we have 2 numbered capture groups 1 and 2. You refer back to these later with the syntax: {R:1} and {R:2}
- The pattern [0-9a-zA-Z\-]+ indicates any character in 0-9, lowercase a-z, uppercase A-Z or "-", one or more repetitions.
- The / is a straight match to a slash "/". In other words, the first named capture group will capture 0-9,a-z, A-Z and "-" prior to the first "/" and you will be able to reference them with {R:1}
- Then we have another named capture group
- The pattern [0-9a-zA-Z\-\.\/\(\)] matches any character in 0-9, lowercase a-z, uppercase A-Z or "-", ".", "/", "(", ")", one or more repetitions.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端