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?
Grab the action from the QueryString, the old school way:
string Action = Request.QueryString["Action"];
Then you can run a case/if statements on it
public ActionResult Index(string Foo)
{
string Action = Request.QueryString["Action"];
if (Action == "DoThis")
{
return Content("Done");
}
else
{
return Content(Action);
}
}
It's one extra line, but it's a very simple solution with little overhead.
Maybe write an HttpModule which renames the action querystring parameter. HttpModules run before MVC gets a hold of the request.
Here'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.
public class SeoModule : IHttpModule
{
public void Dispose()
{ }
public void Init(HttpApplication context)
{
context.BeginRequest += OnBeginRequest;
}
private void OnBeginRequest(object source, EventArgs e)
{
var application = (HttpApplication)source;
HttpContext context = application.Context;
if (context.Request.Url.Query.ToLower().Contains("action=")) {
context.RewritePath(context.Request.Url.ToString().Replace("action=", "actionx="));
}
}
}
I also saw this SO question . It might work for Action I don't know.
How about using plain old ASP.Net? ASP.NET MVC doesnt help in your situation. It is actually in your way.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.