3

I am trying to create a route that only matches if a specific parameter is used in the URL.

For example:

routes.MapRoute(
                name: "BannerAds",
                url: "Go/{Web}",
                defaults: new { controller = "CommunicationsAlias", action = "BannerAds", web = UrlParameter.Optional }
                );

I want the URL to match for http://www.domain.com/Go/Web?=111222

But not for http://www.domain.com/Go/Advertising

How do I change my route to function this way?

4

3 回答 3

3

然后,您需要在路由中将 URL 的那部分设为静态:

routes.MapRoute(
                name: "BannerAds",
                url: "Go/Web",
                defaults: new { controller = "CommunicationsAlias", action = "BannerAds" }
            );

然后将该路线放在更通用的路线之上:

routes.MapRoute(
                name: "BannerAds",
                url: "Go/{Web}",
                defaults: new { controller = "CommunicationsAlias", action = "BannerAds", web = UrlParameter.Optional }
                );
于 2013-05-08T16:47:27.167 回答
0

你应该能够做到这一点:

routes.MapRoute(name: "BannerAds",
                url: "Go/Web",
                defaults: new { controller = "CommunicationsAlias", action = "BannerAds", web = UrlParameter.Optional });

并手动解析控制器中的查询字符串,如下所示:

public ActionResult BannerAds()
{
    string idStr = Request.QueryString.ToString().Trim('='); // strip of leading '='
    int id;
    if (!int.TryParse(idStr, out id))
    {
        return HttpNotFound();
    }

    ...
}
于 2013-05-08T16:47:52.747 回答
0

像这样

routes.MapRoute(
                name: "BannerAds",
                url: "Go/Web",
                defaults: new { controller = "CommunicationsAlias", action = "BannerAds", web = UrlParameter.Optional }
                );

如果你真的只想抓住

http://www.domain.com/Go/Web?x=111222

然后编写控制器来检查查询字符串

编辑

?=111222不是一个正确的查询字符串——我真的不明白你为什么要抓住它——通常有键值对,比如?key=111222or?x=111222写这样你可以检查xor的值,key如果它等于 111222 ,然后做某物

于 2013-05-08T16:48:28.430 回答