如何在请求参数中将Web API 2路由与正斜杠匹配? [英] How to match web api 2 route with forward slashes in request parameters?

查看:88
本文介绍了如何在请求参数中将Web API 2路由与正斜杠匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Web API 2属性路由,并且有一个请求无法正确解决.

I'm using Web API 2 attribute routing and I have a request which is not resolved properly.

[Route("~/foo/{bar?}")]
public void Get(string bar);

我的请求就像: mydomain.me/foo/abc/def

我希望将bar接收为"abc/def",但正斜杠会弄乱路线匹配. 用%2F"替换正斜杠不能解决问题.

I expect to receive bar as "abc/def" but the forward slash messes the route match. Replacing the forward slash with "%2F" doesn't solve the problem.

推荐答案

您可以使用基于通配符的匹配,如下所示:

You could use wildcard based matching like below:

[Route("~/foo/{*bar}")]
public string Get(string bar)

这篇关于如何在请求参数中将Web API 2路由与正斜杠匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆