ASP.NET MVC Tip #17 – How to Run an ASP.NET MVC Application

In this tip, I explain the different options for running an ASP.NET MVC application from Visual Studio 2008. I recommend that you run an ASP.NET MVC application directly from the ASP.NET Development Web Server.

An ASP.NET MVC application works differently than a normal ASP.NET Web Forms application. When you request a URL, there might not be a corresponding page on your hard drive. For example, imagine that you request the following URL:

/Product/Index.aspx

There is no reason to assume that an ASP.NET MVC application contains a Product folder or that the Product folder contains a file named Index.aspx. The Routing module used by ASP.NET MVC, by default, will route this request to a controller named ProductController and invoke the ProductController’s Index() method. It’s up to the Index() method to decide what view, if any, to return to the browser.

For this reason, running an ASP.NET MVC application can be trickier than running a normal ASP.NET Web Forms application. Typically, you do not want to open the current page in the web browser. For example, if you have a file with the path ViewsHomeIndex.aspx open in Visual Studio, and you hit F5 to run your application, then you don’t want the Index.aspx to open in the web browser. The problem is that the path ViewsHomeIndex.aspx does not correspond to the real path for viewing the Index view. Most likely, the real path is /Home/Index.aspx. However, the real path, determined by the URL Routing module, could be anything.

Setting a Start Page

You can control what happens when you hit F5 or Ctl-F5 by modifying your ASP.NET MVC project’s Start Action settings. Right-click your project in the Solution Explorer window and select the menu option Properties. Select the Web tab to view the Start Options (see Figure 1).

Figure 1 – Modifying Start Options

image

There are two sections that you can modify. The Start Action section enables you to specify what happens when you run an application. The options are:

· Current Page – Enables you to run the page currently open for editing in Visual Studio.

· Specific Page – Enables you to set a particular page to run. You can set the page here or you can right-click a page in the Solution Explorer window and select the menu option Set As Start Page.

· Start External Program – Enables you to run an external program.

· Start URL – Enables you to request a URL. This option is typically used when building a Web Services application.

· Don’t open a page – Enables you to do nothing.

By default, an ASP.NET MVC application has the Specific Page option selected. If you run an ASP.NET MVC application, the application will always run the default page (the path /).

An ASP.NET Web Forms application, in contrast, has the Current Page option selected by default. If you have a particular page open in Visual Studio then that page will run.

Notice that you can change the start page for an ASP.NET MVC Application to a particular page. For example, if you want to request the URL /Product/Show/23 when your application starts, then you can enter this URL into the Specific Page input field.

You also can modify the Servers section. This section appears only for ASP.NET MVC Web Application and ASP.NET Web Forms Applications, but not for ASP.NET Web Sites. The most interesting setting in this section is the Enable Edit and Continue setting. This setting is disabled by default. If you enable it, then you can modify code in your application while debugging your application. For example, you can set a breakpoint, hit the breakpoint, modify your code, and click the Continue button and the new changes will take effect.

Running from the ASP.NET Development Server

I originally got the following tip from Brian Henderson. He recommends that you run your ASP.NET MVC application by right-clicking the ASP.NET Development Server icon in the notification area of the Taskbar and selecting Open in Web Browser (see Figure 2). The recommendation is that you open your browser window once and keep it open while developing a website.

Figure 2 – Running from the ASP.NET Development Web Server

image

Follow these steps:

(1) Navigate to the settings for your project’s Start Action by right-clicking your project, selecting Properties, and selecting the Web tab.

(2) Change the Start Setting to Don’t open a page.

(3) Change the Enable Edit and Continue setting to enable it.

(4) Save your new changes by clicking the icon of the floppy.

After you make these modifications, executing Debug, Start Debugging or executing Debug, Start without Debugging will not open a new browser window. After you start running your application, you can view it by opening a new browser from the ASP.NET Development Server (see Figure 2).

What’s the advantage of running your MVC application in this strange way? The chief advantage is that you can keep your browser window open on a particular page. You can keep your MVC application open at all times (even when debugging).

Normally, when you run an ASP.NET MVC application by hitting F5 and opening a new browser window, you must close the browser window when you stop debugging. If you have navigated to a particular page, then you must start over again each and every time that you run your application again. Navigating back to a particular page can be a huge time waster!

If, in contrast, you open your browser from the ASP.NET Development Server, then you can maintain your location within your MVC application. You can even stop your application, set new breakpoints, and run your application again without closing the browser window.

Summary

In this tip, I explained the project options that you can set for running an ASP.NET MVC application. I also recommended that you run your ASP.NET MVC applications from the ASP.NET Development Server so that you can maintain your location in a running ASP.NET web site.

Discussion

  1. http:// says:

    If you plan to run the application in production environment (e.g. IIS), it is better to avoid Development Server and use IIS directly. The Development Server behaves differently in some cases (e.g. handles all requests, has issue with ASP.NET MVC routing of “Default.aspx”).

  2. kalvin says:

    Really very helpfull
    thanks a lot

  3. team your posts are always useful. Thanks for sharing.

  4. fr I tried to mock a call to a Linq to SQL query, but I am struggling.

  5. production environment (e.g. IIS), it is better to avoid Development Server and use IIS directly. The Development Server behaves differently in some cases (e.g. handles all requests, has issue with ASP.NET MVC routing of “Default.aspx”).

  6. Car PC Dev says:

    Without knowing this i have tried hitting f5 and always problematic while debugging. If i am opening from the dev server. should i need to attach to the process for start debugging.

  7. your posts are always useful. Thanks for sharing.

  8. As my previous manager always used to say, its much better to spend an extra hour developing it right than spending a whole day debugging the faulty code.

  9. Diet Pills says:

    very nice post thanks!.

  10. NikeAir says:

    Nice article, very helpful. Thanks!—
    Nike ,Jodon ,kappa shoes are selling on the way..Come on
    …………….Nike shoes ! Addidas ! crazy buying ———————— Nike Shoes || air yeezy

  11. g3 It looks like DataContextExtensions.cs line 45 of the Save method should pass the primaryKeyName through to Update.

  12. Mike says:

    I have no starting page specified and got a 403 error when opened the site with http://localhost/, what could be the reason? It seems that IIS could not find the starting page.

  13. HD Video Converter says:

    As the users of HD Camcorders like Sony, Canon, Panasonic, this HD Video Converter is necessary to help us convert hd Video easily and quickly. The Converter for HD provides several practical editing functions to help you achieve ideal output effect. Trim function is to cut videos into clips which you can just convert and transfer to your player. Crop function helps you remove black bars around the movie. You could use Effect function to adjust video brightness, contrast, saturation and more parameters. More powerful and considerate functions are waiting for you to explore.MKV Converter l FLV Converter l DVD Ripper