简体   繁体   中英

Razor cshtml getting parameters from another cshtml

I am new to C# and Razor v3.

I have a php web app that I am trying to convert to ASP.NET. I decided, mainly due to ease, to use Razor. What I am making is a Single Page App.

The way I have it laid out in PHP is via 3 php files, 1 of which essentially passes the variable values to the main index.php like so, for example

vars.php

    if (isset($_GET["lang"])) {
        $lang = mb_strtolower($_GET["lang"]);
     } else {
        $lang = "el";
    }

and this is how my index.php uses that variable

<html lang=<?php echo "\"".$lang."\"";if($page2go===1) {echo " itemscope itemtype=\"http://schema.org/Article\"";}?>>

Now, everytime index.php is called, I call on vars.php by using

 <?php require_once('./scripts/vars.php');?>

This is how my values are passed into my index.php.

I have found I can do the similar by including my if statements and variable delcarations at the top of my index.cshtml. Like so

@{
    var lang = "";

    if (!String.IsNullOrEmpty(Request.QueryString["lang"]))
    {
        var interior = Request.QueryString["lang"];
        interior.ToLower();
        lang = interior;
    }
    else
    {
        lang = "el";
    }
}

Now I perform a LOT of if operations like that, making my index.cshtml an absolute mess.

Is there a way to pass the variable values, like I do in php by including vars.php?

Thanks a lot for your time.

You should recreate your one-page application to an MVC application. All the request processing (and other complex logic) will take place in the controller action.

The controller action will in turn pass all variables that you need to the view that you have created.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM