2

Does anyone know how to convert the following PHP code to ASP.NET?

<?php
$myFile = "includes/status.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, 1);

$status= $theData;

if ( $status == 0) {
    include('includes/bol_down.php');
} 
    elseif ($status == 1){
    include('includes/login_form_up.php');
}

fclose($fh);
?>

3 Answers 3

1

Probably depends a lot on what's being included. ASP.NET doesn't "include" other code in this manner.

If the code being included is just PHP code, then there's no need for this in ASP.NET. The compiled assembly has all of the available code from the compilation already available.

If the code being included is actual page output, then it's a different model for how you'd accomplish this. The closest analogy would be to conditionally display a user control. Something like this:

if (status == 0)
{
  var myControl = (MyControlType)LoadControl("~/MyControl.ascx");
  myPlaceHolder.Controls.Add(myControl);
}
else if (status == 1)
{
  var myOtherControl = (MyOtherControlType)LoadControl("~/MyOtherControl.ascx");
  myPlaceHolder.Controls.Add(myOtherControl);
}

In this case, myPlaceHolder is an existing control on the page which exists solely as a, well, placeholder in order to add controls dynamically. This is because the page life cycle is different in ASP.NET than in PHP. In PHP the scripts are added in-line, whereas in ASP.NET the user controls are inserted into existing markup structure.

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

Comments

1
int status = theData;

if(status == 0)
{
  Response.WriteFile("includes/bol_down.html");  
}
else if(status == 1)
{
  Response.WriteFile("includes/login_form_up.html");
}

I'm assuming theData is of type int (integer).

Couple things to note. ASP.NET precompiles before deployment which is why you can't dynamically add using include. Instead, you have to write the file directly to the output stream (WriteFile()). Also, the file you include can't contain ASP.NET server side code. If it does, the code won't be executed, it will simply be displayed to the user. There may be an alternative to what you're trying to achieve. You can read more on dynamically including the file here.

Comments

0
<?php

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){ 

    include_once "ez_sql_core.php";
    include_once "ez_sql_mysql.php";
    $db = new ezSQL_mysql('db_user','db_password','db_name','db_host'); 

    $song = $db->get_row("SELECT * FROM songs ORDER BY RAND() LIMIT 1");

    $artist = $song->artist;
    $songname = $song->title;
    $url = $song->url;
    $separator = '|';
    echo $url.$separator.$artist.$separator.$songname;
} 

?>

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.