ASP.NET Web Site or ASP.NET Web Application?

前端 未结 25 2352
自闭症患者
自闭症患者 2020-11-21 08:26

When I start a new ASP.NET project in Visual Studio, I can create an ASP.NET Web Application or I can create an ASP.NET Web Site.

What is the difference between ASP.

相关标签:
25条回答
  • 2020-11-21 08:41

    Compilation Firstly there is a difference in compilation. Web Site is not pre-compiled on server, it is compiled on file. It may be an advantage because when you want to change something in your Web Site you can just download a specific file from server, change it and upload this file back to server and everything would work fine. In Web Application you can't do this because everthing is pre-compiled and you end up with only one dll. When you change something in one file of your project you have to re-compile everything again. So if you would like to have a possibility to change some files on server Web Site is better solution for you. It also allows many developers to work on one Web Site. On the other side, if you don't want your code to be available on server you should rather choose Web Application. This option is also better for Unit Testing because of one DLL file being created after publishing your website.

    Project structure There is also a difference in the structure of the project. In Web Application you have a project file just like you had it in normal application. In Web Site there is no traditional project file, all you have is solution file. All references and settings are stored in web.config file. @Page directive There is a different attribute in @Page directive for the file that contains class associated with this page. In Web Application it is standard "CodeBehind", in Web Site you use "CodeFile". You can see this in the examples below:

    Web Application:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"  
    Inherits="WebApplication._Default" %>  
    

    Web Site:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
    

    Namespaces - In the example above you can see also another difference - how namespaces are created. In Web Application namespace is simply a name of the project. In Website there is default namespace ASP for dynamically compiled pages.

    Edit and Continue- In Web Application Edit and Continue option is available (to turn it on you have to go to Tools Menu, click Options then find Edit and Continue in Debugging). This feature is not working in Web Site.ASP.NET MVCIf you want to develop web applications using

    ASP.NET MVC (Model View Controller) the best and default option is Web Application. Although it's possible to use MVC in Web Site it's not recommended.

    Summary - The most important difference between ASP.NET Web Application and Web Site is compilation. So if you work on a bigger project where a few people can modify it it's better to use Web Site. But if you're doing a smaller project you can use Web Application as well.

    0 讨论(0)
  • 2020-11-21 08:41

    Yes web application is much better than web sites, because Web applications give us freedom:

    1. To have multiple projects under one umbrella and establish project dependencies between. E.g. for PCS we can have following within web application-

      • Web portals
      • Notification Controller (for sending Email)
      • Business layer
      • Data Access layer
      • Exception Manager
      • Server utility
      • WCF Services (Common for all platforms)
      • List item
    2. To run unit tests on code that is in the class files that are associated with ASP.NET pages

    3. To refer to the classes those are associated with pages and user controls from standalone classes
    4. To create a single assembly for the entire site
    5. Control over the assembly name and version number that is generated for the site
    6. To avoid putting source code on a production server. (You can avoid deploying source code to the IIS server. In some scenarios, such as shared hosting environments, you might be concerned about unauthorized access to source code on the IIS server. (For a web site project, you can avoid this risk by pre-compiling on a development computer and deploying the generated assemblies instead of the source code. However, in that case you lose some of the benefits of easy site updates.)
    7. Performance Issue with Website(The first request to the web site might require the site to be compiled, which can result in a delay. And if the web site is running on an IIS server that is short on memory, including the entire site in a single assembly might use more memory than would be required for multiple assemblies.)
    0 讨论(0)
  • 2020-11-21 08:43

    Applications are usually compiled before deployment where as the website makes use of the app_code directory. When anything changes in the app code folder the server will re-compile the code. This means that you can add/ change code with a website on the fly.

    The advantage of an app is that there is no re-compiling and so initial start up times will be faster.

    0 讨论(0)
  • 2020-11-21 08:44

    Website:

    The Web Site project is compiled on the fly. You end up with a lot more DLL files, which can be a pain. It also gives problems when you have pages or controls in one directory that need to reference pages and controls in another directory since the other directory may not be compiled into the code yet. Another problem can be in publishing.

    If Visual Studio isn't told to re-use the same names constantly, it will come up with new names for the DLL files generated by pages all the time. That can lead to having several close copies of DLL files containing the same class name, which will generate plenty of errors. The Web Site project was introduced with Visual Studio 2005, but it has turned out not to be popular.

    Web Application:

    The Web Application Project was created as an add-in and now exists as part of SP 1 for Visual Studio 2005. The main differences are the Web Application Project was designed to work similarly to the Web projects that shipped with Visual Studio 2003. It will compile the application into a single DLL file at build time. To update the project, it must be recompiled and the DLL file published for changes to occur.

    Another nice feature of the Web Application project is it's much easier to exclude files from the project view. In the Web Site project, each file that you exclude is renamed with an excluded keyword in the filename. In the Web Application Project, the project just keeps track of which files to include/exclude from the project view without renaming them, making things much tidier.

    Reference

    The article ASP.NET 2.0 - Web Site vs Web Application project also gives reasons on why to use one and not the other. Here is an excerpt of it:

    • You need to migrate large Visual Studio .NET 2003 applications to VS 2005? use the Web Application project.
    • You want to open and edit any directory as a Web project without creating a project file? use Web Site project.
    • You need to add pre-build and post-build steps during compilation? use Web Application project.
    • You need to build a Web application using multiple Web projects? use the Web Application project.
    • You want to generate one assembly for each page? use the Web Site project.
    • You prefer dynamic compilation and working on pages without building entire site on each page view? use Web Site project.
    • You prefer single-page code model to code-behind model? use Web Site project.

    Web Application Projects versus Web Site Projects (MSDN) explains the differences between the web site and web application projects. Also, it discusses the configuration to be made in Visual Studio.

    0 讨论(0)
  • 2020-11-21 08:47

    In Web Application Projects, Visual Studio needs additional .designer files for pages and user controls. Web Site Projects do not require this overhead. The markup itself is interpreted as the design.

    0 讨论(0)
  • 2020-11-21 08:48

    I recommend you watch the video Web Application Projects & Web Deployment Projects on the ASP.NET website which explains the difference in great detail, it was quite helpful to me.

    By the way, don't get confused by the title, a great part of the video explains the difference between website projects and web application projects and why Microsoft re-introduced Web application projects in Visual studio 2005 (as you probably already know, it originally shipped with only website projects then web application projects were added in SP1). A great video I highly recommend for anyone who wants to know the difference.

    0 讨论(0)
提交回复
热议问题