ViewData VS ViewBag Vs TempData in MVC

Introduction

ViewData and ViewBag are used for the same purpose to transfer data from controller to view. Both life lies only in current request. ViewData is nothing but dictionary of object and it is accessible by string as key. ViewData is property of controller that exposes an instance of the ViewDataDictionary class. ViewBag is very similar to ViewData. ViewBag is a dynamic property (dynamic keyword which is introduced in .net framework 4.0). ViewBag is able to set and get value dynamically and able to add any number of additional fields without converts it to strongly typed. ViewBag is just a wrapper around the ViewData.

ViewData Example

//Controller Code

public ActionResult Index()

{

      List<string> Student = new List<string>();

      Student.Add(“Jignesh”);

      Student.Add(“Tejas”);

      Student.Add(“Rakesh”);

 

      ViewData[“Student”] = Student;

      return View();

}

//page code

<ul>

    <% foreach (var student in ViewData[“Student”] as List<string>)

        { %>

    <li><%: student%></li>

    <% } %>

</ul>

ViewBag Example

 

//Controller Code

public ActionResult Index()

{

      List<string> Student = new List<string>();

      Student.Add(“Jignesh”);

      Student.Add(“Tejas”);

      Student.Add(“Rakesh”);

 

      ViewBag.Student = Student;

      return View();

//page code

<ul>

    <% foreach (var student in ViewBag.Student)

        { %>

    <li><%: student%></li>

    <% } %>

</ul>

TempData is a dictionary which is derived from TempDataDictionary class. TempData is stored data just like live session for short time. TempData Keep data for the time of HTTP Request it mean that it hold data between two consecutive requests. TempData help us to transfer data between controllers or between actions. TempData internally use Session variables. Note that TempData is only work during the current and subsequent request. It is generally used to store one time message. With the help of TempData.Keep() method we can keep value in TempData object after request completion.

TempData Example

//Controller Code

public ActionResult Index()

{

    List<string> Student = new List<string>();

    Student.Add(“Jignesh”);

    Student.Add(“Tejas”);

    Student.Add(“Rakesh”);

 

    TempData[“Student”] = Student;

    return View();

}

//page code

<ul>

    <% foreach (var student in TempData[“Student”] as List<string>)

        { %>

    <li><%: student%></li>

    <% } %>

</ul>


ViewData VS ViewBag VS TempData

ViewData ViewBag TempData
It is Key-Value Dictionary collection It is a type object It is Key-Value Dictionary collection
ViewData is a dictionary object and it is property of ControllerBase class ViewBag is Dynamic property of ControllerBase class. TempData is a dictionary object and it is property of controllerBase class.
ViewData is Faster than ViewBag ViewBag is slower than ViewData NA
ViewData is introduced in MVC 1.0 and available in MVC 1.0 and above ViewBag is introduced in MVC 3.0 and available in MVC 3.0 and above TempData is also introduced in MVC1.0 and available in MVC 1.0 and above.
ViewData  is also work with .net framework 3.5 and above ViewBag  is only  work with .net framework 4.0 and above TempData  is also work with .net framework 3.5 and above
Type Conversion code is required while enumerating In depth, ViewBag is used dynamic, so there is no need to type conversion while enumerating. Type Conversion code is required while enumerating
It value become null if redirection is occurred. Same as ViewData TempData is used to pass data between two consecutive requests.
It lies only during the current request. Same as ViewData TempData is only work during the current and subsequent request


Conclusion

We have three options ViewData, ViewBag and TeampData for passing data from controller to view and in next request. ViewData and ViewBag are almost similar and it helps us to transfer the data from controller to view whereas TempData is also work during the current and subsequent request.

via: http://www.c-sharpcorner.com/Blogs/12427/viewdata-vs-viewbag-vs-tempdata-in-mvc.aspx

How to: Specify a Port for the Development Server

In Visual Studio, you can select from several Web servers: IIS, IIS Express, or the built-in Visual Studio Development Server. For information about the differences between these servers, see Web Servers in Visual Studio for ASP.NET Web Projects.

By default, when you create a Web site or a Web application project in Visual Studio that uses IIS Express or the Visual Studio Development Server, a port is selected for the project. For example, if you are testing a page named MyPage.aspx, when you run the page using IIS Express or the Visual Studio Development Server, the URL of the page might be the following:

http://localhost:31544/MyPage.aspx

If you want to run the project on a specific port, you can configure the Web server to do so.

NoteNote
Visual Studio cannot guarantee that the port you specify will be available when you run your file-system Web site. If the port is in use when you run a page, Visual Studio displays an error message.

To specify a port for a Web site project that uses IIS Express

  1. In the Tools menu, select Options and then General.
  2. Make sure the Always Show Solution option is selected.
  3. In Solution Explorer, right-click the project name and then click Remove. This removes the project from your solution, but does not delete the corresponding files on disk.
  4. Click OK.
  5. In Windows Explorer, navigate to the IIS Express ApplicationHost.config file. By default, this is located in the following folder:%systemdrive%:\Users\<username>\Documents\IISExpress\config
  6. Open the ApplicationHost.config file in a text editor such as Notepad. In the sites section, search for the Web site name.
  7. In the bindings section for the Web site, change the port binding.The following example shows the bound port set to 40955.
    <site name="WebSite1" id="1">
       <application path="/" applicationPool="Clr4IntegratedAppPool">
          <virtualDirectory path="/" physicalPath="C:\WebSites\WebSite1" />
       </application>
       <bindings>
          <binding protocol="http" bindingInformation="*:40955:localhost" />
       </bindings>
    </site>
  8. Save the ApplicationHost.config file and close the text editor.
  9. In Solution Explorer, right-click the solution, select Add, and then select Existing Web Site.
  10. In the Add Existing Web Site dialog box, make sure that the Local IIS tab is selected.
  11. Under IIS Express Sites, select the site for which you have changed the port number, and then click Open.The project name contains the URL with the new port number for the Web site (for example, http://localhost:40955/). If the solution was created at the same time as the original Web site project, the previous port number will still be part of the solution name (for example, localhost_40954).
  12. To update the solution name to reflect the new port number, in Solution Explorer right-click the solution name, select Rename, and then specify a new name.
  13. On the File menu, click Save All.
  14. To verify the change, press CTRL+F5 to run the project.The new port number appears in the address bar of the browser.

To specify a port for a Web site project that uses the Visual Studio Development Server

  1. In Solution Explorer, select the project name.
  2. In the Properties pane, set Use dynamic ports to False.This enables editing of the Port number property.
  3. Enter a port number for the Port number property.
  4. Click outside of the Properties pane. This saves the property settings
  5. To verify the change, press CTRL+F5 to run the project. The new port number appears in the address bar of the browser.

To specify a port for a Web application project that uses IIS Express

  1. In Solution Explorer, right-click the name of the application and then select Properties.
  2. Click the Web tab.
  3. In the Servers section, under Use Local IIS Web server, in the Project URL box change the port number.
  4. To the right of the Project URL box, click Create Virtual Directory, and then click OK.
  5. In the File menu, click Save Selected Items.
  6. To verify the change, press CTRL+F5 to run the project.The new port number appears in the address bar of the browser.

To specify a port for a Web application project that uses the Visual Studio Development Server

  1. In Solution Explorer, right-click the name of the application, and then select Properties.
  2. Click the Web tab.
  3. In the Servers section, under Use Visual Studio Development Server, select Specific port.
  4. Change the port number.
  5. In the File menu, click Save Selected Items.
  6. To verify the change, press CTRL+F5 to run the project.The new port number appears in the address bar of the browser.

via: http://msdn.microsoft.com/en-us/library/ms178109.ASPX

Troubleshooting badly behaving IIS application pools

There are many reasons why an application pools’ worker process (W3WP.exe) could be behaving badly.  The best approach is to capture some memory dumps during the problem situation and then analyze them.  The problem with that is the root cause is not always obvious, even after many hours of analysis and investigation.

An alternative to memory dump analysis, or maybe a prequel, is to capture and analyze the behavior of the worker process.  One of the simplest actions to monitor the application pool is to open up Task Manager and look at the PID of the W3WP.exe process, as illustrated in Figure 1.

Figure 1, the w3wp.exe PID

Write down the PID in the morning and then come back a few times during the day to see if the process is restarting.  When/if the worker process restarts, it will get a new PID.  If you do notice that the worker process is being restarted, consider using the “Generate Recycle Event Log Entry” features available via the Advanced Settings… window for the given application pool.  Figure 2 shows the default settings on a Windows Server 2012 IIS 8 machine.

Figure 2, The Generate Recycle Event Log Entry settings

During the troubleshooting phase you might consider enabling all of the attributes to be certain that you capture as much information as possible.  However, once you complete your troubleshooting phase, consider disabling the ones you do not find useful in
your environment.  The description of these attributes are described in the below bulleted list.

  • Application Pool Configuration Changed – Event is logged when the application pool recycles due to a change in its configuration
  • ISAPI Report Unhealthy – Event is logged because an ISAPI extension has reported itself as unhealthy
  • Manual Recycle – Event is logged when the application pool has been manually recycled
  • Private Memory Limit Exceeded – Event is logged when the application pool recycles after exceeding its private memory limit
  • Regular Time Interval – Event is logged when the application pool recycles on its scheduled interval
  • Request Limit Exceeded – Event is logged when the application pool recycles after exceeding its request limit
  • Specific Time – Event is logged when the application pool recycles at a scheduled time
  • Virtual Memory Limit Exceeded – Event is logged when the application pool recycles after exceeding its virtual memory limits

Once configured, you would begin seeing events logged into the System event logs on the IIS server.  Figure 3 shows 3 logged events which caused a recycle of the application pools’ worker process.

  • Event ID 5186, 5080 and 5079
  • Useful link to IIS Application Pool Recycling event ids

Figure 3, IIS Application Pool Recycling events

The recycling of an application pool is in many cases nothing to be alarmed about.  However, this is a place to analyze and track when you are experiencing some unexpected behavior.  At the same time, if the recycling is happening multiple times throughout the day, you should look into what is causing it to happen as the recycle does have a customer impact.  Read this article which digs deeper into appDomain recycles and the impact they can have here.

via: http://blogs.msdn.com/b/benjaminperkins/archive/2013/07/01/troubleshooting-badly-behaving-iis-application-pools.aspx