Web Design, Programming, Tutorials
Posts tagged ASP.NET MVC
ASP.NET MVC – Organizing your solution
Feb 4th
Keeping your project files organized will help you navigate through your projects and help you find what you are looking for to make changes faster.
Here are some tips that I use to help keep my ASP.NET MVC solutions organized:
- Use multiple projects to separate sections of the program.
- Use folders to group interfaces, classes, and factories that deal with an object.
- If objects can be used by other programs, put them in their own class library project.
Option 1
When beginning a new program, I usually start by creating an ASP.NET MVC solution with 4 projects:
- The main project – which contains the MVC components of the application such as Controllers, Views, CSS, and JavaScript files.
- The unit testing project – which contains all of the requirements for the application along with any fake repositories used for testing.
- The data project – which contains the repositories and domain objects used by the application.
- The services project – which contains the service objects that are the middle-man between the Controller and the Repository.
Option 2
Folders are used within the projects to further organize the code files within. The data project could have a domain object called Operation. Domain objects are placed in a folder with the same name as the object. Interfaces, Repositories, and Factories that deal with the object are also placed in the domain object folder.
The service project is organized similar to the data project.
The unit test project is slightly modified from the default to create a folder which contains all fake repository objects and another folder to contain requirement tests. Depending on the detail of unit testing you wish to utilize, you may have unit tests that verify application requirements and/or unit tests that further verify other parts your code.
In my opinion, you can write application requirements that detail exactly what the program should do and as long as you unit test these requirements, you don’t need to waste time trying to unit test 100% of your program. If a bug is found later, it will most likely be because it was not an application requirement. Then, add a new requirement, write a unit test, and make the application pass the new test.
Option 3
Where I work, we have various databases that we need to communicate with. We wanted a way to use these databases the same way every time without having to remember all of the different objects and names that needed to be used by each. So, I developed a parent wrapper class around the basic database functionality and then individual wrappers around each database object that inherited from the parent wrapper. Now, we can call up any of the database connections and work with them in exactly the same way, the only difference between them is the call to the factory when creating the instance.
These database wrappers were added to their own separate project so that in future applications, we could continue to reuse them.
Configuring IIS 6 and ASP.NET MVC
Jan 28th
By default, IIS 6 does not work with ASP.NET MVC and needs to be configured to use wild-card mapping to get MVC’s routing and clean URLs to work correctly. Unfortunately, IIS 6 does take a performance hit because all requests are processed by ASP.NET. Static files, such as images, CSS, and JavaScript are processed as a dynamic page instead of a static one.
Install ASP.NET MVC
Follow the first two steps of the instructions for installing ASP.NET MVC. The third step can be ignored since Visual Studio will not be installed on the server.
Configuring ASP.NET 2.0
First thing to do is open the IIS Manager ->expand your server ->Web Service Extensions folder. Right-click on the white space under the list of Web Service Extensions and select to Add a new Web service extension…
The New Web Service Extension window will pop open. Enter “ASP.NET v2.0.50727″ as the Extension Name and click the Add button. Browse to the C:\Windows\Microsoft.Net\Framework\v2.0.50727 folder and select the aspnet_isapi.dll file. Make sure to check the “Set extension status is Allowed” button before clicking OK.
This will allow your programs to run ASP.NET 2.0, 3.0, and 3.5 versions of the .NET framework.
Configuring Your Web Application
The next thing to do is open the properties of the Web Site where your web application is. Select the ASP.NET tab and select 2.0.50717 in the ASP.NET version drop down box.
Next, click on the Home Directory tab. Click on the Configuration button. Under the Wild-card application maps area, click the Insert button. Browse to the C:\Windows\Microsoft.Net\Framework\v2.0.50727 folder and select the aspnet_isapi.dll file. Uncheck the “Verify that file exists” before clicking OK. Save your changes and your site should now be able to run ASP.NET MVC and use the routes setup by your application.
Scheduling A Task In A Web Application On A Windows Server
Jan 19th
When writting web applications, there may be times when you would like to perform some task automatically at the same time or every X number of hours. Web applications only execute their code when a request is made from a client’s browser. Because of this, scheduled tasks within the web application will not work.
There are a few different options:
- Build a separate Windows Service application that can connect to the assemblies your web application uses to execute the scheduled task.
- Build a separate Windows application that communicates with a web service that your web application also uses to perform the scheduled task.
- Use the Windows Scheduler to call the URL of the action to perform the task.
The first option might sound good at first, but creating a separate program that talks to your web application’s library files is a bad idea. Maintenance will become a problem as you change your web application. Since the code is in two places, unit testing will not tell you that the class library you just changed has now broken your scheduled tasks in your windows service.
The second option is slightly better, but you still may have issues with changing your class library.
The third option is the one that I will show you how to configure. We will setup a VBS script that will perform a call to any web address we pass to it. Then, we’ll setup a batch file that calls the VBS script and passes in one or more URLs to execute. And finally, we’ll go through using the Windows Scheduler to create the scheduled task when the batch file should execute.
The primary advantage to this method is that you get to keep all of your code together in your web application. You simply setup an action that performs whatever task you wish to schedule and make sure that action is callable from an anonymous user.
WebRequest.vbs
You may copy the following code and paste into a text file. Then, rename the file to WebRequest.vbs. The script basically takes an argument that is passed into it as the URL. The HTTP object is setup and then the URL is called using a GET request. Then, the returned status is checked to see if the request was successful or not.
url = WScript.Arguments.Item(0)
'WScript.echo url
set WshShell = WScript.CreateObject("WScript.Shell")
set http = CreateObject("Microsoft.XmlHttp")
http.open "GET", url, FALSE
http.send ""
if (http.Status = 200) then
WScript.echo "Request successful."
else
WScript.echo "Error: " & http.Status
end if
set WshShell = nothing
set http = nothing
The Batch File
Calling the WebRequest.vbs file from a batch file is very easy. Simply create a new text file, calling it whatever you’d like, and paste in the following line of text. Rename the file to .BAT when finished.
cscript WebRequest.vbs "http://www.domain.com/controller/action"
Scheduling The Task
From your Windows server, open the Control Panel and then open the Scheduled Tasks item. Next, click on Add Scheduled Task. Choose any application to schedule. This will be changed later on. Configure how often you wish the schedule to run and enter a user name and password to run the schedule as. This users should have access to running the web browser from the server.
After your new scheduled task has been created, open it. In the Run text box, type in the full path to your batch file. Then, in the Start In text box, type in the full path without the batch file name. You can also adjust your scheduled time from within here. Click OK to save the changes and your scheduled task should be ready.
You can test the Windows Scheduler by setting the scheduled time to a minute or two in the future and then waiting for it to execute. If you just wish to test your action, or manually run it, you can simply run the batch file.
Installing ASP.NET MVC
Jan 15th
Here are the steps to install into Visual Studio 2008:
- Download ASP.NET MVC 1.0 from Microsoft’s website. Make sure to download the MSI file and the source code if you would like to see how it works.
- Install the AspNetMVC1.msi file.
- Open Visual Studio 2008 and go to:
- File -> New -> Project
- Under Project Types, select Web under either Visual Basic or C#
- Under Templates, select ASP.NET MVC Web Application
- Give your application a name and change its location if you wish
That’s it! Upon creating a new web application, the default MVC application will open.
Note:
ASP.NET MVC should also work using the free development tool from Microsoft, Visual Web Designer 2008.
Why you should use ASP.NET MVC
Jan 14th
If you’re like me, you like to have control over how your websites look and the HTML returned to the browser. ASP.NET MVC gives you that control by letting you keep your HTML separate from the core application code. If you learn to use tools like CSS and JavaScript, you can use them in the exact same way you would if you were working with a static web page. There are no generated IDs or missing/custom HTML attributes. While it does give you some helpers to generate HTML, you do not have to use them if you don’t want to.
ASP.NET MVC is very extensible and flexible. Nearly any component of the framework can be swapped out for your own version, if you so choose to delve into the inner workings.
ASP.NET MVC was built with Unit Testing in mind. If you’re not using Unit Testing, you should definitely start researching how it can improve your code reliability.
So, why should you use ASP.NET MVC?
- Based on the MVC pattern.
- Allows you to use Standards such as XHTML and CSS.
- Separation of concerns allows for more maintainable code.
- Gives you help when you need it and gets out of your way when you want control.
- Unit Testing is built in and easy to do.
- Fully supported by Microsoft.
- You can download the code for the ASP.NET MVC Framework and see how it works.
If you know HTML, CSS, or JavaScript and you are familiar with ASP, then you should definitely look into ASP.NET MVC.