Export (0) Print
Expand All

Testing Web Pages in Visual Studio

Visual Studio provides you with various ways to test web pages as you are developing them. You have the following choices:

  • Start Debugging (F5).  This option runs the web project in the browser and allows you to perform debugging tasks such as stepping through code and stopping at breakpoints.

  • Start Without Debugging (CTRL+F5).  This option runs the web project in the browser without using the debugger. It approximates the user experience.

  • View in Browser (CTRL+SHIFT+W).  This option runs a selected page in the browser. Debugging breakpoints are not in effect even if debugging is enabled.

By default, Visual Studio uses your default browser to test pages. To use a different browser or Page Inspector, right-click the page in Solution Explorer and then click either Browse With or View in Page Inspector. The Browse With lets you select a browser from a list, add new browsers to the list, or set one as the default browser. (The default browser setting here applies only to the Visual Studio environment and not to Windows.)

By default, Visual Studio builds the web project when you run a page. Building the project helps you find compile-time errors before the page is displayed in the browser. You can set options to specify whether you want to build the entire project, the current page, or not build at all.

For more information, see Building (Compiling) Web Site Projects.

By default, when you run a web project, Visual Studio runs the page that is currently open. However, in some cases you might always want to start your web project with a specific page. For example, your web project might have a home page where users make a selection that is required for other pages in the site or application. In this case, you can configure your web project to display that page first when you test run your application.

To specify a start page, right-click the page in Solution Explorer and then click Set As Start Page.

Note Note

Setting a start page in Visual Studio affects only your testing. It does not set the start page for the website when it is published. To specify a default page for your website, first use Internet Information Services (IIS) Manager to enable a default document for your website or virtual directory. Then, select the page that you want. For more information, see the topic Configuring Default Documents in IIS 7.

When you run a page, Visual Studio launches the web server, launches the browser, and loads the specified page into the browser. In effect, Visual Studio passes the URL of the current page to the web server, and the web server runs the page that was requested.

The web server that is used to run the page depends on the type of website you are working with:

  • For File system web projects, Visual Studio launches its own web server and runs the page locally. However, you can also specify a start URL to use when testing a web page. This method is useful if you want to test a file system website on another computer with IIS. For more information about the ASP.NET Development Server, see Web Servers in Visual Studio for ASP.NET Web Projects.

  • For local IIS Express, local IIS, and remote IIS sites, Visual Studio runs the page in the corresponding instance of IIS or IIS Express. For more information, see Web Servers in Visual Studio for ASP.NET Web Projects.

  • For FTP-deployed websites, Visual Studio runs the page by using the start URL that you provide as part of the FTP website properties. If you have not provided one, Visual Studio prompts you for the start URL when it is required. For more information, see FTP-Deployed Web Site Projects.

For a remote website or FTP site, the remote computer must be running the same .NET Framework version that you are developing for.

Running your page with the debugger enables you to perform debugging tasks, such as stepping through code and stopping at breakpoints. For more information, see ASP.NET Debugging Overview.

Running with the debugger requires the following:

  • Configure the page or web project to enable debugging. This option compiles the page with extra information (debugging symbols) that the debugger uses. Visual Studio checks to see if debugging is enabled. If it is not, Visual Studio sets the compilation options in the local Web.config file to include debugging, creating the Web.config file if necessary.

  • Configure the web server with adequate permissions to allow the current user account to debug. If you are debugging a page that is running under IIS, you must be an administrator on the computer. If that is not practical, you can use IIS Express or the ASP.NET Development Server to debug applications without special permissions. For details, see Web Servers in Visual Studio for ASP.NET Web Projects and How to: Specify the Web Server for Web Projects in Visual Studio.

Note Note

Configure your pages or website to run without debugging before you deploy the site to a production server. Debugging options add overhead to the page and can affect server performance.

You can test pages when you are developing web application projects or website projects in Visual Studio.

To test run a web page

  • Perform one of the actions listed in the following table. All of the actions run the specified page in the default browser.

    To

    Do this

    Run the current page or startup page in the debugger.

    For information about the startup page, see the section How to: Specify a Startup Page for a Web Site.

    On the Debug menu, click Start Debugging, or press F5.

    If the page or the website has not previously been configured to enable debugging, Visual Studio prompts you to enable debugging. If you choose not to enable debugging, the page runs in the default browser but does not allow you to attach a debugger to it.

    Note Note

    Before you publish a website, disable debugging. Debugging adds overhead to performance and could display information to the user that you do not want to reveal. For details, see How to: Enable Debugging for ASP.NET Applications

    Run the current page or startup page without the debugger.

    For information about the startup page, see the section How to: Specify a Startup Page for a Web Site.

    On the Debug menu, click Start Without Debugging, or press CTRL+F5.

    Run the current page in a browser instance inside of Visual Studio.

    Right-click a blank area of the page and then click View in Browser.

    Note Note

    The View in Browser command is not available for files that do not render in a browser (such as the Web.config file).

When you run a page, Visual Studio invokes the default browser. You can also test a page by using an alternate browser, as described in the following procedure.

To test a web page with an alternate browser

  1. In Solution Explorer, right-click the page you that want to test and then click Browse With.

  2. In the Browse With dialog box, select an alternate browser.

  3. If the browser that you want to use is not listed, click Add, specify the alternate browser program, and then click OK.

    Note Note

    You can set the alternate browser as the default browser for Visual Studio by clicking Set as Default. This option sets the default browser only for testing web pages in the current website. It does not affect the default browser setting in Windows.

  4. When you have selected a browser, click Browse.

Visual Studio can build the current page or the entire website when you run a page. Building helps you find compile-time errors before the page appears in the browser. You can set options to specify whether you want to build the entire site, the current page, or not build at all.

To specify build behavior when running a page in a website project

  1. In Solution Explorer, right-click the name of the website that you want to run, and then click Property Pages.

  2. Click the Build tab.

  3. In the Before running the startup page list, select the build option that you want before running pages.

If you have configured the website to build before running a page, you can further specify what happens if the page or website has errors.

To specify run behavior if build errors occur in a website or Web application project

  1. On the Tools menu, click Options.

  2. Select the Show all settings check box if it is not already selected.

  3. Expand the Projects and Solutions node, and then click Build and Run.

  4. Under On Run, when build or deployment errors occur, select the option for behavior that you want if the build process encounters an error.

When you use Start Debugging or Start Without Debugging to run a web project, Visual Studio runs the page that currently has the focus. However, you can set a start page to have your website or Web application always begin with the same page. If you set a start page, the Start Debugging or Start Without Debugging commands always run the page that you set, not the current page.

Setting a start page in Visual Studio affects only your testing. It does not set the start page for the web project when it is published. To specify a default start page for a website, use Internet Information Services (IIS) to enable a default document and specify the page. For more information, see Configuring Default Documents in IIS 7.

Note Note

If you are working with a website for ASP.NET Web Pages (.cshtml files), use the procedure for website projects.

To specify a start page for testing a web application project in Visual Studio

  1. In Solution Explorer, under the Web application name, double-click the Properties folder.

    -or-

    In Solution Explorer, right click the Web application name, and then from the shortcut menu, choose Properties.

  2. On the Web tab, in the Start Action section, choose Specific Page.

  3. Type the name of the file of the start page.

-or-

  • In Solution Explorer, right-click the page that you want to specify as the start page, and then click Set As Start Page.

To specify a start page for testing a website project in Visual Studio

  1. In Solution Explorer, right-click the name of the website for which you want to specify the start page, and then click Property Pages.

  2. In the Start Options tab, select the Specific page check box and then type or browse to the name of the start page.

-or-

  • In Solution Explorer, right-click the page that you want to specify as the start page, and then click Set As Start Page .

Show:
© 2014 Microsoft