1

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?

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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="));
        }
    }
}

Comments

0

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

Comments

0

How about using plain old ASP.Net? ASP.NET MVC doesnt help in your situation. It is actually in your way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.