asp.net-mvc – 如何使用“?”路由查询字符串以及如何处理它

在我的全局asax文件中,我想映射一个这样的路由:
http://domain.com/add/link?url=http%3A%2F%2Fgoogle.com

然后使用我的LinkController和名为Add的操作捕获它.

我这样做吗?

global.asax->

routes.MapRoute(
    "AddLink","Add/Link?{url}",new { controller = "Link",action = "Add" }
);

LinkController->

public string Add(string url)
{
    return url; // just want to output it to the webpage for testing
}

??这似乎不起作用.我究竟做错了什么?谢谢!

解决方法

ASP.Net MVC将自动绑定查询字符串中的参数;你不需要把它放在路线上.

你的路线可以简单

routes.MapRoute(
    "AddLink","Add/Link",action = "Add" }
);

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....