Dha*_*alR 5 c# asp.net-mvc-routing asp.net-mvc-4 attributerouting
我Attribute Routing在MVC4申请中使用.我已设定路线[Route("test-{testParam1}-{testParam2}")].这里`{testParam2}'可能包含'test'这个词.例如,如果我输入如下的网址,
localhost:33333/test-temp-test-tempparam2
Run Code Online (Sandbox Code Playgroud)
这给了我404错误.在url中,这里{testParam2}有两个单词test tempparam2格式化为test-tempparam2.当test单词位于最后位置时{testParam2},它运行良好.这意味着如果网址像.../test-temp-tempParam2-test运行良好.但是下面给出错误..../test-temp-test-tempParam2.
以下是可能有帮助的代码......
[Route ("test-{testParam1}-{testParam2}")]
public ActionResult Foo (int testParam2) {...}
Run Code Online (Sandbox Code Playgroud)
现在尝试关注两个网址.
localhost:(port)/test-temp-1
localhost:(port)/test-test-temp-1
在我的情况下,第二个错误.在这种情况下第一参数被格式化为test-temp从test temp.首先运行良好.
如何解决这个问题呢?
OP表明路由模板中的最后一个参数是int
使用路线约束。
路由约束允许您限制路由模板中参数的匹配方式。一般语法是{parameter:constraint}. 例如:
[Route ("test-{testParam1}-{testParam2:int}")]
public ActionResult Foo (int testParam2) {...}
Run Code Online (Sandbox Code Playgroud)
因此,当尝试以下两个 URL 时。
[Route ("test-{testParam1}-{testParam2:int}")]
public ActionResult Foo (int testParam2) {...}
Run Code Online (Sandbox Code Playgroud)
第一个将匹配路线数据{testParam1 = temp}-{testParam2 = 1}
第二个将匹配路线数据{testParam1 = test-temp}-{testParam2 = 1}
| 归档时间: |
|
| 查看次数: |
990 次 |
| 最近记录: |