Razor, Web-page Model, Web-Application Development in C# #.Net Web Applications

Brought from: http://salearningschool.com/displayArticle.php?table=Articles&articleID=1320&title=Razor,%20Web-page%20Model,%20Web-Application%20Development%20in%20C# (written long back)

Razor, Web-page Model, Web-Application Development in C#

  • Razor:a markup syntax to add server side code into ASP.net pages
  • Razor example: Current time is @DateTime.Now
  • Razor Code Syntax
               
             Single statement block
         @{ var message = "Hello World"; }
    
        Inline expression or variable
        The message: @message
    
        Multi-statement block
        @{
            var greeting = "Hello";
            var currentTime = DateTime.Now;
            var greetMessage = greeting + " Current time is: " + currentTime;
        }
        The greeting is: @greetMessage
        
  • Some Razor functions mainly used in the Layout file [can be used in other files based on the function]: RenderPage() - render the content of a page, RenderBody(), RenderSection(section) - render the content of a section named section
  • using _ at the beginning of a web-page name prevents it from being browsed from browser such as _header.cshtml, _Layout.cshtml
  • In ASP.net, you can hide sensitive information by using the _AppStart.cshtml file. Here, you can keep database passwords, and email passwords. You can also keep your webmail server configuration.
  • _AppStart.cshtm example
          
                      @{
                WebMail.SmtpServer = "smtp.bizmail.yahoo.com";
                WebMail.EnableSsl = true;
                WebMail.UserName = "sayed@justetc.net";
                WebMail.Password = "password";
                WebMail.From = "sayed@justetc.net";
            }
                
  • Some folders as used in ASP.net web applications: App_Data - to keep database files, Images - image files, Scripts - JavaScript files for example, Shared - common files such as CSS files
  • In a file path, ~ is used to indicate the virtual root folder.
  • Server.MapPath, can convert a virtual path to a physical path
  • @Href(cssPath), here cssPath is a variable for a path. You can use Href(cssPath) for the value of href attributes
  • Page object methods: href (builds a url), RenderBody(), RenderPage(page), RenderSection(section), Write (object) - HTML Encoded string, WriteLiteral - No HTML encoding
  • Page object properties: isPost, Layout, Page, Request, Server
  • You can add properties to the Page object such as Page.Title; here Title is your own variable [not built in]
  • Read from a physical file: File.ReadAllLines(filePath). You may want to use var filePath = Server.MapPath("~/file.txt");, to get the physical path from a virtual path
  • File.ReadAllLines(filePath), can also be used to read excel csv files [comma delimited]
  • Some ASP.net helpers: Helpers are just components. WebGrid, Chart, WebMail, and WebImage are some common helpers
  • WebGrid helper example: webGrid = new WebGrid (data); grid.getHtml(); data for example can be the rows of a database table, GetHtml () - will render the data as a HTML table.
  • Chart example
                    @{
                        var db = Database.Open("dbName");
                        var dbdata = db.Query("SELECT Name, Price FROM Product");
                        var myChart = new Chart(width: 600, height: 400)
                           .AddTitle("Product Sales")
                           .DataBindTable(dataSource: dbdata, xField: "Name")
                           .Write();
                }
    
                
  • The above example will create bar chart [column chart]
  • if you want a pie chart as output, use .AddSeries(chartType:"Pie" in the above example
  • You can use an Array, or XML data as the data feed for the Chartff
  • Sending emails: WebMail.Send(to:"xyz@xyz.com", subject: "Email from - " + customerEmail, body: request
  • Publish your web-site: Use the publish option from your IDE (Webmatrix, Visual Studio for Web)
  • Manual publish: Make sure that the server side has the compatible ASP.net framework, copy all of your folders and files, copy the DLL files into the bin folder, copy SQL server DLL files, copy the database if appropriate

From: http://sitestree.com/?p=3720
Categories:.Net Web Applications
Tags:
Post Data:2016-07-16 10:49:27

    Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
    (Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a> 
    In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
    <a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
    8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
    Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
    Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>

Leave a Reply