garaber
garaber

Reputation: 232

Getting posted values from asp server with out forms

I am not to sure about the way to form this question. I'm new to Web development. We are going to post data to a server over asp. In the example I'm playing with in Visual Studio 2012. I've created an empty project with controllers. But how do I get the string after the path. Request file path is associated with a form and this will be view-less.

Update

public class HomeController : Controller
{
    public HttpRequestBase baseRequest;

    public void Index()
    {
        if (Request.QueryString.Count > 0 && Request.QueryString[0] != null)
        {
            String surlQuery = Request.QueryString["varname"].ToString();
        }
    }

    protected override IAsyncResult BeginExecute(System.Web.Routing.RequestContext requestContext, AsyncCallback callback, object state)
    {
        return base.BeginExecute(requestContext, callback, state);
    }
}

The Request.Query String is null with the project option for url set to http://localhost:53792/test 5

Upvotes: 0

Views: 90

Answers (1)

Kemo Sabe
Kemo Sabe

Reputation: 99

Request.QueryString is an array of parameters passed in the URL. To pass in variable you'll need to add a question mark and variable/value pairs into your url.
Example:

example.com?variable=value&nothervar=notherval

If you're referring to passing in vars without ? and = like stack does it:

website.com/variable/value

That would be an .htaccess solution. Something like this in your .htaccess file:

RewriteEngine On 
RewriteRule ^variable/([^/]*)$ /?variable=$1 [L]

Don't forget to always check to make sure it's not null before trying to use it.

if (Request.QueryString.Count > 0 && Request.QueryString[0] != null)

or you can use the string name of the variable as well:

Request.QueryString["varname"]

You will have to cast/parse/convert it to whatever data type you need.

Request.QueryString["varname"].ToString();
int h = Int32.Parse(Request.QueryString["varname"].ToString());

Also, just want to point out that "post" is incorrect in this regard. POST refers to things passed in using a form or other such that utilizes the POST protocol. Whereas, variable in the URL are called GET.

Upvotes: 2

Related Questions