Crating an MVC Project from a Template

Starting an Empty MVC Project

You can create a complete MVC website from scratch. If you do, the amount of work to set up the project can be overming. Otherwise, or to assist you in starting a project, Microsoft Visual Studio provides two basic templates.

To get a basic project starting project, you can ask Microsoft Visual Studio to create all starting folders for your website. To do this, start an ASP.NET Web Application. In the New ASP.NET Application dialog box, select the Empty option and click the MVC check box. After you have clicked OK, Microsoft Visual Studio would create all the primary folders of an MVC website and a Web.config file:

Solution Explorer - Empty ASP.NET MVC Basic Project

Once the semi-empty project has been created, normally, one of the things you should do sooner or a later is to create at least one controller. After getting a controller, you can create and associate the desired view(s) to it. Besides the controller(s), you can add the classes and files you judge necessary to complement the project. Of course, you should create those classes and files in the appropriate folders.

Practical LearningPractical Learning: Starting a Semi-Empty Project

  1. Start Microsoft Visual Studio
  2. On the main menu of Microsoft Visual Studio, click File -> New Project...
  3. In the New Project dialog box, make sure ASP.NET Web Application (.NET Framework) is selected in the middle list.
    Change the Name of the project to Chemistry1
  4. Click OK
  5. In the New ASP.NET Web Application, click the Empty icon.
    Click the MVC check box

    Dialog Box - New ASP.NET Web Application

  6. Click OK
  7. In the Solution Explorer, right-click Controllers -> Add -> Controller...
  8. In the middle frame of the Add Scaffold dialog box, make sure MVC 5 Controller - Empty is selected. If not, click it.
    Click Add
  9. In the Add Controller dialog box, type Home to get HomeController
  10. Click Add
  11. Add three new methods as follows:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace Chemistry1.Controllers
    {
        public class HomeController : Controller
        {
            // GET: Home
            public ActionResult Index()
            {
                return View();
            }
    
            // GET: ElementSearch
            public ActionResult ElementSearch()
            {
                return View();
            }
    
            // GET: Compounds
            public ActionResult Compounds()
            {
                return View();
            }
    
            // GET: PeriodicTable
            public ActionResult PeriodicTable()
            {
                return View();
            }
        }
    }
  12. In the Solution Explorer, under Views, right-click Home -> Add -> New Scaffold Item...
  13. In the middle frame of the Add Scaffold dialog box, click MVC 5 View
  14. Click Add
  15. In the Add View dialog box, change the View Name to ElementSearch
  16. Click Add
  17. In the Solution Explorer, right-click Home -> Add -> View...
  18. In the Add View dialog box, type Compounds for the View Name
  19. Click Add
  20. Change the document as follows:
    . . . No Change
    
    <h2>Chemical Compounds</h2>
  21. In the Solution Explorer, right-click Home -> Add -> View...
  22. Type PeriodicTable for the View Name and click Add
  23. Change the document as follows:
    @{
        ViewBag.Title = "Periodic Table";
    }
    
    <h2>Periodic Table</h2>
  24. In the Solution Explorer, under Views and under Shared, double-click _Layout.cshtml to open it
  25. Change the document as follows:
    <!DOCTYPE html>
    <html>
    <head>
        . . . No Change
    </head>
    <body>
        <div class="navbar navbar-inverse navbar-fixed-top">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    @Html.ActionLink("Home", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
                </div>
                <div class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        <li>@Html.ActionLink("Element Search", "ElementSearch", "Home")</li>
                        <li>@Html.ActionLink("Compounds", "Compounds", "Home")</li>
                        <li>@Html.ActionLink("Periodic Table", "PeriodicTable", "Home")</li>
                    </ul>
                </div>
            </div>
        </div>
    
        <div class="container body-content">
            @RenderBody()
            <hr />
            <footer>
                <p>&copy; @DateTime.Now.Year - Chemistry Lessons</p>
            </footer>
        </div>
    
        <script src="~/Scripts/jquery-1.10.2.min.js"></script>
        <script src="~/Scripts/bootstrap.min.js"></script>
    </body>
    </html>
  26. In the Solution Explorer, under Views, right-click the Home sub-folder -> Add -> View...
  27. In the View Name text box of the Add View dialog box, replace the name with Index
  28. Click Add
  29. Just in case it has lost focus, click inside the Index document to give it focus.
    To execute the project, on the main menu, click Debug -> Start Without Debugging:

    Introducing Interfaces

  30. Click the different links
  31. Close the browser and return to your programming environment

A Semi-Complete Project

Microsoft Visual Studio provides a semi-complete ASP.NET MVC project from which you can start a website. To get it, after starting an ASP.NET Web Application, in the New ASP.NET Web Application, click the MVC icon. If necessary, you can click the buttons and other check boxes for more options. When you are ready, click OK.

Practical LearningPractical Learning: Creating a Semi-Complete Project

  1. On the main menu of Microsoft Visual Studio, click File -> New -> Project...
  2. In the middle frame of the New Project dialog box, make sure ASP.NET Web Application (.NET Framework) is selected (if not, click it).
    Change the Name of the project to OFRE2
  3. Click OK
  4. In the New ASP.NET Web Application dialog box, in the list of templates, click MVC

    New ASP.NET Web Application

  5. Click OK
  6. To execute the application, on the main menu, click Debug -> Start Without Debugging:

    Introducing Interfaces

  7. Click a link in the top section, such as Contact:

    Introducing Interfaces

  8. Close the browser and return to your programming environment

Customizing a Project from a Microsoft Visual Studio Template

Introduction

An ASP.NET website you create from a template is primarily a project like any other. If necessary, you can add any new folder(s) or file(s) you judge necessary.

Practical LearningPractical Learning: Adding Folders and Files to a Project

  1. Open the Chemistry08 project you created earlier
  2. In the Solution Explorer, right-click Chemistry08 -> Add -> Add ASP.NET Folder -> App_Code
  3. In the Solution Explorer, right-click App_Code -> Add -> Class
  4. Type the name of the file as Element
  5. Click Add
  6. Populate the document as follows:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Chemistr08.App_Code
    {
        public enum Phase
        {
            Gas,
            Liquid,
            Solid,
            Unknown
        }
    
        public class Element
        {
            public string Symbol { get; set; } = "H";
            public string ElementName { get; set; } = "Hydrogen";
            public int AtomicNumber { get; set; } = 1;
            public decimal AtomicWeight { get; set; } = 1.008M;
            public Phase Phase { get; set; } = Phase.Gas;
    
            public Element()
            {
            }
    
            public Element(int number)
            {
                AtomicNumber = number;
            }
    
            public Element(string symbol)
            {
                Symbol = symbol;
            }
    
            public Element(int number, string symbol, string name, decimal mass)
            {
                Symbol = symbol;
                ElementName = name;
                AtomicWeight = mass;
                AtomicNumber = number;
            }
        }
    }

Customizing the Design of a Microsoft Visual Studio ASP.NET Project

If you create an Empty ASP.NET Web Application and select the MVC check box in the New ASP.NET Application dialog box, when you create your first controller, Microsoft Visual Studio would add a layout file and three starting CSS files to your project. If you create an MVC project by selecting the MVC icon in the New ASP.NET Application dialog box, Microsoft Visual Studio would add all the necessary start folders and files. In both cases, you can change various aspects of the template as you see fit.

One of the aspects you may want to customize for a project created from a template is the design. The design of a typical ASP.NET website involves the layout document, the CSS file(s), and the webpages. In all these categories of files, you have three options:

Practical LearningPractical Learning: Customizing the Design of an MS Visual Studio Project

  1. In the Solution Explorer, under the Content folder, double-click Site.css to open it
  2. Add the following items to the end of the document:
    .top-banner {
        background-color: #003366;
    }
    
    #main-title {
        font-weight: 800;
        margin-top: 6px;
        font-size: 2.52em;
        text-align: center;
        color: blue;
        border-bottom: 1px solid blue;
        font-family: Garamond, 'Times New Roman', Georgia, serif;
    }
    
    .elm-presentation {
        width: 350px;
        margin: auto;
        padding-top: 20px;
    }
    
    .small-text { width:       40px;  }
    .emphasize  { font-weight: bold;  }
    .left-col   { width:       120px; }
  3. Access the _Layout file and find the line that contains <div class="navbar navbar-inverse navbar-fixed-top">
  4. On the line, replace navbar-inverse with top-banner
    <!DOCTYPE html>
    <html>
    <head>
    
        . . . No Change
    
    </head>
    <body>
        <div class="navbar top-banner navbar-fixed-top">
            <div class="container">
    
                . . . No Change
    
            </div>
        </div>
    
        <div class="container body-content">
            @RenderBody()
            <hr />
            <footer>
                <p>&copy; @DateTime.Now.Year - Chemistry Lessons</p>
            </footer>
        </div>
    
        <script src="~/Scripts/jquery-1.10.2.min.js"></script>
        <script src="~/Scripts/bootstrap.min.js"></script>
    </body>
    </html>
  5. Access the ElementSearch.cshtml file and change its document as follows:
    . . . No Change
    
    @{
        Chemistr08.App_Code.Element h = new Chemistr08.App_Code.Element(1, "H", "Hydrogen", 1.008M) { Phase = Chemistr08.App_Code.Phase.Gas };
        Chemistr08.App_Code.Element he = new Chemistr08.App_Code.Element(2, "He", "Helium", 4.002602M) { Phase = Chemistr08.App_Code.Phase.Gas };
        Chemistr08.App_Code.Element li = new Chemistr08.App_Code.Element(3, "Li", "Lithium", 6.94M) { Phase = Chemistr08.App_Code.Phase.Solid };
        Chemistr08.App_Code.Element be = new Chemistr08.App_Code.Element(4, "Be", "Beryllium", 9.0121831M) { Phase = Chemistr08.App_Code.Phase.Solid };
        Chemistr08.App_Code.Element b = new Chemistr08.App_Code.Element(5, "B", "Boron", 10.81M) { Phase = Chemistr08.App_Code.Phase.Solid };
        Chemistr08.App_Code.Element c = new Chemistr08.App_Code.Element(name: "Carbon", mass: 12.011M, symbol: "C", number: 6) { Phase = Chemistr08.App_Code.Phase.Solid };
        Chemistr08.App_Code.Element n = new Chemistr08.App_Code.Element(7);
        n.Symbol = "N";
        n.AtomicWeight = 14.007M;
        n.ElementName = "Nitrogen";
        n.Phase = Chemistr08.App_Code.Phase.Gas;
    
        Chemistr08.App_Code.Element o = new Chemistr08.App_Code.Element("O")
        {
            AtomicNumber = 8,
            ElementName = "Oxygen",
            AtomicWeight = 15.999M,
            Phase = Chemistr08.App_Code.Phase.Gas
        };
        Chemistr08.App_Code.Element f = new Chemistr08.App_Code.Element("F")
        {
            AtomicNumber = 9,
            ElementName = "Fluorine",
            AtomicWeight = 15.999M,
            Phase = Chemistr08.App_Code.Phase.Gas
        };
        Chemistr08.App_Code.Element ne = new Chemistr08.App_Code.Element("Ne")
        {
            AtomicNumber = 10,
            ElementName = "Neon",
            AtomicWeight = 20.1797M,
            Phase = Chemistr08.App_Code.Phase.Gas
        };
        Chemistr08.App_Code.Element na = new Chemistr08.App_Code.Element(11, "Na", "Sodium", 22.98976928M) { Phase = Chemistr08.App_Code.Phase.Solid };
        Chemistr08.App_Code.Element mg = new Chemistr08.App_Code.Element(12, "Mg", "Magnesium", 24.305M) { Phase = Chemistr08.App_Code.Phase.Solid };
        Chemistr08.App_Code.Element al = new Chemistr08.App_Code.Element(13, "Al", "Aluminium", 26.9815385M) { Phase = Chemistr08.App_Code.Phase.Solid };
        Chemistr08.App_Code.Element si = new Chemistr08.App_Code.Element() { ElementName = "Silicon", AtomicWeight = 28.085M, Symbol = "Si", AtomicNumber = 14, Phase = Chemistr08.App_Code.Phase.Solid };
        Chemistr08.App_Code.Element p = new Chemistr08.App_Code.Element() { ElementName = "Phosphorus", AtomicWeight = 30.973761998M, Symbol = "P", AtomicNumber = 15, Phase = Chemistr08.App_Code.Phase.Solid };
        Chemistr08.App_Code.Element s = new Chemistr08.App_Code.Element(16, "S", "Sulfur", 32.06M) { Phase = Chemistr08.App_Code.Phase.Solid };
        Chemistr08.App_Code.Element cl = new Chemistr08.App_Code.Element(17, "Cl", "Chlorine", 35.45M) { Phase = Chemistr08.App_Code.Phase.Gas };
    
        int atomicNumber = 0;
    
        if (IsPost)
        {
            atomicNumber = Request["txtNumberEntered"].AsInt();
    
            switch (atomicNumber)
            {
                default:
                    selected = h;
                    break;
                case 2:
                    selected = he;
                    break;
                case 3:
                    selected = li;
                    break;
                case 4:
                    selected = be;
                    break;
                case 5:
                    selected = b;
                    break;
                case 6:
                    selected = c;
                    break;
                case 7:
                    selected = n;
                    break;
                case 8:
                    selected = o;
                    break;
                case 9:
                    selected = f;
                    break;
                case 10:
                    selected = ne;
                    break;
                case 11:
                    selected = na;
                    break;
                case 12:
                    selected = mg;
                    break;
                case 13:
                    selected = al;
                    break;
                case 14:
                    selected = si;
                    break;
                case 15:
                    selected = p;
                    break;
                case 16:
                    selected = s;
                    break;
                case 17:
                    selected = cl;
                    break;
            }
        }
    }
    
    <p id="main-title">:: Chemistry ::</p>
    
    <div class="elm-presentation">
        <form name="frmChemistry" method="post">
            <table>
                <tr>
                    <td class="left-col emphasize">Enter Atomic #:</td>
                    <td>
                        <input type="text" name="txtNumberEntered" class="small-text" value="@atomicNumber" />
                        <input type="submit" name="btnFind" value="Find" style="width: 70px;" />
                    </td>
                </tr>
                <tr>
                    <td class="emphasize">Symbol:</td>
                    <td><input type="text" name="txtSymbol" value=@selected.Symbol /></td>
                </tr>
                <tr>
                    <td class="emphasize">Element Name:</td>
                    <td><input type="text" name="txtElementName" value=@selected.ElementName /></td>
                </tr>
                <tr>
                    <td class="emphasize">Atomic Weight:</td>
                    <td><input type="text" name="txtAtomicWeight" value=@selected.AtomicWeight /></td>
                </tr>
                <tr>
                    <td class="emphasize">Phase:</td>
                    <td><input type="text" name="txtAtomicWeight" value=@selected.Phase /></td>
                </tr>
            </table>
        </form>
    </div>
  6. Access the Index.cshtml file and click inside the document to give it focus
  7. To execute the project, on the main menu, click Debug -> Start Without Debugging.
  8. Just in case it has lost focus, click inside the Index document to give it focus.
    To execute the project, on the main menu, click Debug -> Start Without Debugging:

    Customizing the Design of a Microsoft Visual Studio ASP.NET Project

  9. Click the different links

    Customizing the Design of a Microsoft Visual Studio ASP.NET Project

  10. Close the browser and return to your programming environment
  11. Save the following pictures to your computer:


    Campaign Campaign
    Campaign
  12. Open the OFRE2 project you created earlier
  13. In the Solution Explorer, right-click OFRE2 -> Add -> New Folder
  14. Type Images and press Enter
  15. Add the above pictures to the Images folder
  16. In the Solution Explorer, right-click Controllers -> Add -> New Scaffolded Item...
  17. In the Add Scaffold dialog box, click MVC 5 Controller - Empty
  18. Click Add
  19. Type Campaigns to get CampaignsController
  20. Click Add
  21. Change the class as follows:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace OFRE2.Controllers
    {
        public class CampaignsController : Controller
        {
            // GET: TwoWay
            public ActionResult TwoWay()
            {
                return View();
            }
    
            // GET: ThreeWay
            public ActionResult ThreeWay()
            {
                return View();
            }
        }
    }
  22. In the Solution Explorer, expand Views
  23. In the Solution Explorer, right-click Controllers -> Add -> New Scaffolded Item...
  24. In the Add Scaffold dialog box, click MVC 5 View
  25. Click Add
  26. Type TwoWay for the View Name
  27. Click Add
  28. In the Solution Explorer, right-click Campaigns -> Add > View...
  29. Type ThreeWay for the View Name
  30. Press Enter
  31. In the Solution Explorer, expand the Content folder and double-click Site.css to open it
  32. Add the following styles at end of the document:
    #glued-part {
        top: 0;
        width: 100%;
        height: 75px;
        position: fixed;
        background-color: white;
    }
    #heading1 {
        color: maroon;
        font-weight: 800;
        font-size: 2.52em;
        text-align: center;
        line-height: 2.05em;
        font-family: Cambria, Cochin, Georgia, Times, Times New Roman, serif;
    }
    .navbar-fixed-top {
        top: 75px;
    }
    
    .navbar-inverse {
        background-color: #581405;
        border-top: 3px solid black;
        border-bottom: 3px solid black;
    }
    
    .jumbotron {
        padding-top: 80px;
        margin-bottom: 0px;
        background-color:  white;
        border: 1px solid burlywood
    }
  33. In the Solution Explorer, under Content, double-click bootstrap.css to open it
  34. Find the following section in the document:
    @media screen and (min-width: 768px) {
      .jumbotron {
        padding-top: 48px;
        padding-bottom: 48px;
      }
      .container .jumbotron {
        padding-right: 60px;
        padding-left: 60px;
      }
      .jumbotron h1 {
        font-size: 63px;
      }
    }
  35. In that section, change the value of padding-botton from 48px to 4px
  36. In the Solution Explorer, under Views, expand Shared
  37. Double-click _Layout.cshtml to open it
  38. Change the document as follows:
    <!DOCTYPE html>
    <html>
    <head>
        . . . No Change
    </head>
    <body>
    <div id="glued-part">
        <p id="heading1">Organization for Fundraising Events</p>
    </div>
    
    <div class="navbar navbar-inverse navbar-fixed-top">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    @Html.ActionLink("OFRE", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
                </div>
                <div class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        <li>@Html.ActionLink("Home", "Index", "Home")</li>
                        <li>@Html.ActionLink("About OFRE", "About", "Home")</li>
                        <li>@Html.ActionLink("Contact Us", "Contact", "Home")</li>
                    </ul>
                </div>
            </div>
    </div>
    
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <h4 class="text-center">&copy; @DateTime.Now.Year :: Organization for Fundraising Events</h4>
        </footer>
    </div>
    
        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/bootstrap")
        @RenderSection("scripts", required: false)
    </body>
    </html>
  39. In the Solution Explorer, under Views, expand Home
  40. Double-click Index.cshtml to open it
  41. Change the document as follows:
    . . . No Change
    
    <div class="jumbotron">
        <h1>OFRE</h1>
        <p class="lead">
            The Organization for Fundraising Events, or OFRE, is a group of people who assist organizations, 
            communities, associations, schools, causes, religious institutions, etc, to plan, conduct, promote, 
            and run campaigns to raise money for any goal (cause, health issues, communities, school, politics, 
            ceremony, religion, catastrophe, business startup, etc). The Organization can provide a complete 
            service, including promoting the event, collecting, and delivering the funds. OFRE is a private, 
            independent, and a not-for-profit organization.
        </p>
    </div>
    
    <div class="row">
        <div class="col-md-4">
            <p class="pic-holder"><img src="~/Images/Campaign.png" alt="Plan With Us" width="248" height="137" /></p>
            <h2>Plan With Us</h2>
            <p>Are you ready for a memorable experience in fundraising? You reaached the right place. 
            Do you lack experience in fundraising? Don&#039;t take a chance. Don&#039;t worry about 
            a thing. Let us run your next campaign. Give us the opportunity to put our vast 
            experience at your disposal. You will enjoy the whole adventure.</p>
        </div>
        <div class="col-md-4">
            <p class="pic-holder"><img src="~/Images/TwoWay.png" alt="Two-Way Campaign" width="248" height="136" /></p>
            <h2>Two-Way Campaign</h2>
            <p>This is an already completely created program to run a campaign to raise funds for two 
            organizations or causes. This program consists of starting a compaign, collecting funds, 
            appropriately distributing them, and delivering a complete legal/business/financial 
            report of the campaign.</p>
            @Html.ActionLink("Two-Way Campaign", "TwoWay", "Campaigns", new { DontTryToFindOutWhatThisIs = "" }, new { @class = "btn btn-default" })
        </div>
        <div class="col-md-4">
            <p class="pic-holder"><img src="~/Images/ThreeWay.png" alt="Three-Way Distribution" width="248" height="136" /></p>
            <h2>Three-Way Distribution</h2>
            <p>In this program, the Organization runs a campaign to raise funds for three different 
            organizations or causes. The ratios of distribution are stated any way a group wants. 
            An effective computer application is intuitively ready to process all types of requests.</p>
            @Html.ActionLink("Three-Way Campaign Distribution", "ThreeWay", "Campaigns", new { ThisIsJustAPlaceHolder = "" }, new { @class = "btn btn-default" })
        </div>
    </div>
  42. Access the Index.cshtml file
  43. To execute the project, on the main menu, click Debug -> Start Without Debugging:

    Customizing the Design of a Microsoft Visual Studio ASP.NET Project

  44. Close the browser and return to your programming environment

The Objects of a Controller/View System

Getting the HTTP Responses of a Web Page

To give you access to the HTTP response of a webpage, the WebPageRenderingBase class provides a property named Response whis is of type HttpResponseBase. Actually, the HttpResponseBase class is an adaptation of the HttpResponse class, which is an object of the Microsoft Internet Information Server (IIS).

Getting the Server of a Web Site

To give you access to the server and server-related operations of a website, the WebPageRenderingBase class provides a property named Server. This property is of type HttpServerUtilityBase, which is an adaptation of the Server class of IIS.

The Context of a View

To display its content in a browser, a view gathers colors and fonts. It identifies the patths of the pictures. It creates a list of the necessary HTML tags and their styles to figure out the formatting of text, the alignment of the paragraphs, and the positions of the pictures or webcontrols. The view must then draw text and download the pictures. If there is code such as JavaScript or else involved with the HTML code, the view must interpret that code and act appropriately. All these details and others (such as security, etc) are groupped in what is called the context of a view.

To support the context of a view, the .NET Framework provides a class named ViewContext. The ViewContext class is derived from a class named ControllerContext that has no parent (except the object class from which all classes of a C# project are derived). Both the ViewContext and the ControllerContext classes are defined in the System.Web.Mvc namespace of the System.Web.Mvc.dll library. Since ViewContext is a normal class, you can declare a variable of it. Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>Exercise</title>
</head>
<body>

@{
    System.Web.Mvc.ViewContext vc = new System.Web.Mvc.ViewContext();
}

</body>
</html>

On the other hand, you can create a method that produces a ViewContext object. Here is an example:

using System.Web.Mvc;

public class ResearchAndDevelopment
{
    public ViewContext Create()
    {
        ViewContext context = new ViewContext();

        return context;
    }
}

You can also create a parameter of its type. Here is an example:

using System.Web.Mvc;

public class ResearchAndDevelopment
{
    public void Draw(ViewContext context)
    {
        
    }
}

Operations on a View Page

Writting to a View

To let you write values to a view, the HttpResponseBase class is equipped with an overloaded method named Write . You can pass it a string because one of its versions used the following syntax:

public virtual void Write(string s)

Here is an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Exercise.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            Response.Write("Welcome to our website.");

            return View();
        }
    }
}

One of the characteristics of the HttpResponseBase.Write() method is that its string can include HTML tags. Here is an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Exercise.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            Response.Write("<h1>Chemistry Lessons</h1>");

            return View();
        }
    }
}

The HTML tag(s) in the argument can also contain attributes and/or styles. In these case, the value(s) of the attribute(s) should be included in single quotes. Here is an example:

using System.Web.Mvc;

namespace Exercise.Controllers
{
    public class CampaignsController : Controller
    {
        public ActionResult Index()
        {
            Response.Write("<h1>Organization for Fundraising Events</h1>" +
                           "<p style='font-size: 12px; color: #0000FF;'>OFRE is in the business " +
                           "of helping organizations and causes that want/need to raise money.</p>");

            return View();
        }
    }
}

Redirecting to a Web Page

.Redirecting to a webpage consists of automatically moving the browser's focus from one webpage to another. There are various ways you can take casre of this.

To support webpage redirection, the HttpResponseBase class is equipped with an overloaded method named Redirtect. One of the versions takes a string as argument. Its syntax is:

public virtual void Redirect(string url)

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2017-2021, FunctionX Next