本文介绍了如何使用“?"路由查询字符串以及如何处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的全局asax文件中,我想映射这样的路由:

In my global asax file, I want to map a route such as this:

http://domain.com/add/link?url=http%3A%2F%2Fgoogle.com

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

And then catch it using my LinkController with action called 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
}

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

?? That doesn't seem to work. What am I doing wrong? Thanks!

推荐答案

ASP.Net MVC将自动绑定查询字符串中的参数.您无需将其放置在路线中.

ASP.Net MVC will automatically bind parameters from the query string; you don't need to put it in the route.

您的路线可以简单地

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

这篇关于如何使用“?"路由查询字符串以及如何处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 09:18