1

I am new at C#, but i work in PHP, i need help for some equivalent PHP code to C# here is my code

if(isset($_GET['something'] == 'today')) {

    $test = $_GET['something'];
} else {
    $test = '';
}

How is possible to write that in C#?

9
  • We do not work in php and we do not know what is this. You must ask what you want in C# rather than giving your php code. Commented Jul 14, 2015 at 5:59
  • Nice but i dont ask you if you dont know, i asked if someone knows Commented Jul 14, 2015 at 6:01
  • Since you have PHP in your mind but try to work in ASP.NET MVC which has different concepts, you should really have some basic lessons at asp.net/mvc Commented Jul 14, 2015 at 6:02
  • Yup bro, But this will decrease the probability of Getting a good answer. Commented Jul 14, 2015 at 6:02
  • @Rohit But there are some developers that work in both Php and C# like myself. I guess he didn't just ask the right way. Commented Jul 14, 2015 at 6:03

3 Answers 3

5

Request.QueryString is equivalent to PHP's $_GET in C#. The QueryString collection retrieves the values of the variables in the HTTP query string.

string test = Request.QueryString["something"];
        if (test == "today")
        {
            // we've got test logic
        }
        else
        {
           test = string.Empty;
        }
Sign up to request clarification or add additional context in comments.

Comments

0

$_GET in php is just server side variable.

In ASP.NET MVC you have 3 rigth places where you can store and get this variables:

ViewData - Dictiorary

ViewData.Model - Strongly Typed and the most right way

ViewBag - Dynamic type storrage.

Also, you can get this params from HttpContext.Request.QueryString like this:

HttpContext.Request.QueryString["something"]

But you should never do it in ASP.NET MVC.

1 Comment

Mate $_get can also be contoller action, but i have so simple app that i dont need action for this
-1

ASP.NET MVC just doesn't work like this, if you have to write such a if-clause in ASP.NET MVC you don't understand it.

Basically, you would have some "action" like public ActionResult Index(string something == "today") { ... } where you can easily check the variable etc. it will be automatically fetched from your "routes" and http request context.

There are many good learning resources at http://www.asp.net/mvc like free Learning Videos from Pluralsight.

2 Comments

Mate PHP also can be MVC, it is also Controller Action, but for my app i dont need that, i dont know why you are trying to explain me what is controller action, I didnt asked that :(
@play2web because it's the way ASP.NET MVC is designed, as I show you in the Example, the Variable is automatically resolved from the "Request.QueryString" when using something as simple as a Controller Action. And if you don't have Controller Actions, you obviously don't do ASP:NET MVC, but at time of my responsed, you had that as a Tag in your question

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.