Many of us do not think much about choosing a right Visual Studio project template for new development.
It may not come under practice, or neither your superior might have stressed much to follow it, but it comes with experience to choose the right project. It is as much important as any other practice we follow in coding standards, best practices and performance optimizations at company wide.
Why choosing a right project is important? Here are the examples:
Scenario 1: There are situations where the requirement is an application that should be triggered from a Scheduled Job.
Let’s say for example, the project template chosen for this requirement is Web application, as it also does the same job. If it is web application, obviously it is an .ASPX page that needs to be scheduled from Scheduler software.
This works without any issues, as any other application, but there are some disadvantages in tracking the logs, accessing the page etc. An .ASPX page needs to be accessed from browser, so the scheduler program needs to trigger the browser and then refer to the path. If the web site is hosted on different server, which is typical in any corporate environment to have separated environments for development, staging and production, then there is a need of internet connection to access the .ASPX page.
There are many things that happen between a client browser application and the page that is hosted on web server, IIS in this case. And of course, this is fine when we really need an application that can be accessed from anywhere, and by many users. But this is not a right choice for the project that is hosted somewhere and triggered by a scheduler software.
The right choice in this case is a “Console Application” template in Visual studio, which ultimately an .EXE project. It can be triggered by executing the .EXE file and it can be scheduled easily from any scheduler software.
Let us take another example, which most of the developers get confused in choice.
Scenario 2: Require a web site the can serve multiple users by accessing remotely.
The immediate thing that strikes in mind is go to Visual studio and select File > New > Web Site… project. The selection is as simple as single step, but it obviously not.
This is the biggest mistake that any developer doing without knowing the pros and cons of the template that should be used for your web site requirement.
So the question here is which one is the best, Web site project or Web application project?
Sometimes I think why Microsoft has created this confusion, why they have introduced a lazy coding practice with Web site projects.
We can see the differences between these two templates here:
But let me explain the summarized differences between these two, with my comments inside marked in bold text.
Summary of Differences:
|Area||Web application projects||Web site projects|
|Project file structure||A Visual Studio project file (.csproj or .vbproj) stores information about the project, such as the list of files that are included in the project, and any project-to-project references.
It is just a single click to open the solution. Click on .sln file that comes by default.
|There is no project file (.csproj or .vbproj). All the files in a folder structure are automatically included in the site.
No specific file to open the project from its path, however, we can create a solution and add the project to enable single click open solution.
It is a general practice followed in any program. Write your code and publish when you want to deploy. This in my view that, it is clean and straight forward way of deploying.
You can pre compile the site (compile in advance on a development computer or on the server).
In the same article that these references are copied from, an advantage under using Web site project says :
“You like to keep your source code on the production server because it can serve as an additional backup copy.”
I do not understand why keeping the code open to everyone is an advantage. Also, if one wants to keep a backup, why they choose production server?
It violates all the security and it is very difficult to hide/restrict access code to someone who has access to server.
I personally feel this is the biggest disadvantage of choosing “Web Site” template.
Another fact is that we can pre compile it to hide the source code and deploy the assemblies, again this has disadvantages of easy updates to site.
|Namespaces||Explicit namespaces are added to pages, controls, and classes by default.||Explicit namespaces are not added to pages, controls, and classes by default, but you can add them manually.|
Less work and more advantages.
Might be easy in few scenarios, but more work to deploy and manage on servers.
I always prefer to choose “Web Application” template for new development considering the advantages it has in deploying and publishing code to servers. Though “Web Site” template has advantages in debugging and managing code, those are not much helpful when it comes to other factors like security and best practices.
In a note, even Microsoft recommends to use “Web application”
project template over “Web site” template.
|For new development, we recommend that you choose web application projects. This topic explains that web site projects have some advantages, but many developers who choose web site projects eventually find that the disadvantages outweigh any perceived advantages. In addition, as new ASP.NET features are developed, they won’t always be made available for web site projects. For example, the next Visual Studio release after Visual Studio 2012 will have new tooling for creating web projects, and this new tooling will work only with web application projects. For more information, see Creating an ASP.NET Web Project in Visual Studio 2013.|
So next time when you open Visual studio, go for File > New Project > Web > select the type of project, instead, and I am sure you will see the benefits.
Happy coding! 🙂