ASP.NET MVC URl Routing: How to deal with ?Action=Test parameter
NickName:Peterdk Ask DateTime:2010-01-24T01:16:24

ASP.NET MVC URl Routing: How to deal with ?Action=Test parameter

I am required to implement a simple webapp for a online competition for a simple game. I need to handle a Get request and respond to that.

I thought, let's just use a bare ASP.Net MVC app, and let it handle the URL.

Problem is, the URL I need to handle is :

 http://myDomain.com/bot/?Action=DoThis&Foo=Bar

I tried:

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

Problem is, the string Action always gets set as the action name of the route. I always get:

Action == "Index"

It looks like ASP.Net MVC overrides the Action parameter input, and uses the actual ASP.Net MVC Action.

Since I cannot change the format of the URL that I need to handle: is there a way to retrieve the parameter correctly?

Copyright Notice:Content Author:「Peterdk」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/2123951/asp-net-mvc-url-routing-how-to-deal-with-action-test-parameter

Answers
Omar 2010-01-23T17:35:12

Grab the action from the QueryString, the old school way:\n\n string Action = Request.QueryString[\"Action\"];\n\n\nThen you can run a case/if statements on it\n\npublic ActionResult Index(string Foo)\n{\n string Action = Request.QueryString[\"Action\"];\n if (Action == \"DoThis\")\n {\n return Content(\"Done\");\n }\n else\n {\n return Content(Action);\n }\n}\n\n\nIt's one extra line, but it's a very simple solution with little overhead.",


jessegavin 2010-01-23T17:19:52

Maybe write an HttpModule which renames the action querystring parameter. HttpModules run before MVC gets a hold of the request.\n\nHere's an quick and UGLY example. Ugly because I don't like the way I am replacing the parameter name, but you get the idea.\n\npublic class SeoModule : IHttpModule\n{\n public void Dispose()\n { }\n\n public void Init(HttpApplication context)\n {\n context.BeginRequest += OnBeginRequest;\n }\n\n private void OnBeginRequest(object source, EventArgs e)\n {\n var application = (HttpApplication)source;\n HttpContext context = application.Context;\n\n if (context.Request.Url.Query.ToLower().Contains(\"action=\")) {\n context.RewritePath(context.Request.Url.ToString().Replace(\"action=\", \"actionx=\"));\n }\n }\n}\n",


jessegavin 2010-01-23T17:36:32

I also saw this SO question. It might work for Action I don't know.",


More about “ASP.NET MVC URl Routing: How to deal with ?Action=Test parameter” related questions

ASP.NET MVC URl Routing: How to deal with ?Action=Test parameter

I am required to implement a simple webapp for a online competition for a simple game. I need to handle a Get request and respond to that. I thought, let's just use a bare ASP.Net MVC app, and let...

Show Detail

URL-ROUTING for Asp.Net MVC Action parameter binding

I am using ASP.NET MVC 3 for the first time. I want to call a controller action with a single parameter. this parameter is an object, not a simple type. Let's say: Controller = "Person", Action="A...

Show Detail

ASP.NET MVC routing parameter

asp.net mvc routing pattern is {"some_parameter/{controller}/{action}/{id}"} Is this a valid format if some_parameter can be null or string empty

Show Detail

How to use shortened url with asp.net mvc routing?

I'd like to understand how to set routing parameters to do the follow: when user call shortened url like http://hostname.com/shortenedurl my asp.net mvc project should call action and parameter lik...

Show Detail

Asp.Net MVC area parameter in config section

Do we need area parameter as manditory in routing configuration in asp.net mvc rounting? What if we do not give area as parameter at all? With Area parameter: routes.MapRoute( ...

Show Detail

ASP.net MVC Url.Action Link formatting

I'm very new to ASP.net and recently converting from asp.net to asp.net MVC projects. I have one basic question in asp.net MVC Url.Action. I have one ActionResult method with two parameters Quest...

Show Detail

Replace character in url before routing in ASP.NET MVC

Can I manipulate the url before routing it, i.e. before MVC goes through my route configuration to find the route to use. I'd like to replace some characters in the url, for example "www.test.com/...

Show Detail

strange behaviour of MVC routing

I was reading How MVC routing work from Routing Basics in ASP.NET MVC. It says Additional URL Parameters other than {controller} and {action} are available to be passed as arguments to the

Show Detail

asp.net mvc routing: how to use default action but non-default parameter?

I'm rewriting the question, as the answers so far show me that I have not defined it good enough. I'll leave the original question for reference below. When you set up your routing you can specify

Show Detail

asp.net mvc6 / angular2 routing

I have asp.net mvc6 project. On client side i want to use angular2 to create single page application. It means i have two places where i can define routing. As far it's SPA, routing has to be on an...

Show Detail