ASP.NET MVC URl路由:如何处理?Action = Test参数

Pet*_*rdk 1 c# parameters url asp.net-mvc

我需要为简单游戏的在线竞赛实施一个简单的webapp.我需要处理Get请求并对此做出响应.

我想,让我们使用一个简单的ASP.Net MVC应用程序,让它处理URL.

问题是,我需要处理的URL是:

 http://myDomain.com/bot/?Action=DoThis&Foo=Bar
Run Code Online (Sandbox Code Playgroud)

我试过了:

public ActionResult Index(string Action, string Foo)
    {
        if (Action == "DoThis")
        {
            return Content("Done");
        }
        else
        {
            return Content(Action);
        }
    }
Run Code Online (Sandbox Code Playgroud)

问题是,字符串Action总是被设置为路径的动作名称.我总是得到:

Action == "Index"
Run Code Online (Sandbox Code Playgroud)

看起来ASP.Net MVC会覆盖Action参数输入,并使用实际的ASP.Net MVC Action.

由于我无法更改我需要处理的URL的格式:有没有办法正确检索参数?

Oma*_*mar 5

从QueryString中获取动作,旧学校方式:

 string Action = Request.QueryString["Action"];
Run Code Online (Sandbox Code Playgroud)

然后你可以运行一个case/if语句

public ActionResult Index(string Foo)
{
    string Action = Request.QueryString["Action"];
    if (Action == "DoThis")
    {
        return Content("Done");
    }
    else
    {
        return Content(Action);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个额外的行,但它是一个非常简单的解决方案,只需很少的开销.