asp.net-mvc - 需要将 URL 参数传递给 Web API
问题描述
我有一个 MVC 应用程序,其中包含大多数类型的控制器System.Web.MVC.Controller
。也有一个 Web API 控制器类型System.Web.Http.ApiController
。
Web API 控制器有一个动作需要接受 URL 作为参数,如下所示:
[HttpGet]
public HttpResponseMessage Image(string path)
{
//download and return image using WebClient library
}
在视图上我想使用类似的Image
动作
<image src="/api/image/?path=/remote/relative/path/someimage.jpg" alt="some description" />
我正在使用以下路线模板:
config.Routes.MapHttpRoute(
name: "api-image",
routeTemplate: "/api/image/{path}",
defaults: new { controller = "WebApiClient", action = "Image" }
);
Image 动作只被调用路径像/api/image/someimage-1.jpg
或/api/image/someimage-2.jpg
问
:Image
对于长路径,不会调用/api/image/?path=/remote/relative/path/someimage.jpg
如果我用HttpUtility.UrlEncode
,它只会转到全局 Not Found 操作(通过 web.config 配置)。
但是,如果我不对其进行编码,则会引发“路径中发现的危险字符”异常。
我究竟做错了什么?
解决方案
尝试添加*
到路由模板:
config.Routes.MapHttpRoute(
name: "api-image",
routeTemplate: "/api/image/{*path}",
defaults: new { controller = "WebApiClient", action = "Image" }
);
并在不编码的情况下使用它。
推荐阅读
- php - 来自 PHP 的 wget 错误 400,来自命令行的 200
- php - 使用array_filter过滤多维数组
- php - Menu WordPress 获取导航菜单项
- java - Spring webclient,有多少个实例?
- python - 每次在 python 中使用 subprocess.call 发送 linux 电子邮件时都会得到一个空的正文
- mysql - 如果需要所有列,是否应该组合指向同一个表的外键?
- git - 推送后更改特定分支中过去提交的作者和电子邮件
- git - Git 按文件类型合并
- android - 禁用 Android OTA 固件更新
- java - 如何为 AnimatorSet 设置动画结束监听器