This article will discuss the connection of the MVC3 library in Visual Studio 2010. But we will not use the “ASP.NET MVC3 Web Application” project template, but create a Web site (Web site) based on the “Empty Web Site” template, then eat from scratch. We will connect the library to an empty Web site, since There is no MVC template for the Web site.
At the end of the article, a JSON controller based on the MVC library will be implemented.
I would explain the pictures, but they are not inserted. Therefore, I will explain in words.
Prehistory
Recently I was interested in the implementation of MVC in Visual Studio. On the Internet, and in particular on Habré, there are many articles on how to use it, but reviewing 20 pieces of instructions, I found that all of them are based only on the “ASP.NET MVC3 Web Application” template. The first two steps in most instructions are as follows:
- Download and install MVC3.
- Select item:
File -> New -> Project ... -> Installed Templates -> Visual C # -> Web -> “ASP.NET MVC3 Web Application” -> Ok.
But what if a person wants to create a “web site” and not a “web application”. Then he clicks the following menu items:
File -> New -> Web Site -> Installed Templates -> Visual C #
And then it looks for where this MVC3 is: “I just downloaded it and installed it”.
If you take a good look, it’s not for nothing that MVC3 was installed: a new “ASP.NET Web Site (Razor)” template appeared. Razor is certainly cool, but MVC was not in it.
So, it turns out that the MVC project for the Web site is not.
In addition, I confess that I didn’t particularly like the “ASP.NET MVC3 Web Application” template, because it contains too much of itself. For me, it would be enough to include the necessary libraries there and create a directory structure. It is more convenient for me when I control the contents of the project.
Therefore, it was easier for me to create an MVC project from scratch based on the Empty Web Site template.
I will not go into details of what is better to use: "Web site" or "Web Application". To be honest, I am not sure that I fully represent their advantages and disadvantages. But be that as it may, the version of the new project “Web site” in Visual Studio is. Therefore, it is used. Therefore, someone will want to use MVC based on it.
The only thing that confused me was that there were some features in the “ASP.NET MVC3 Web Application” template that I couldn’t realize in the Web Site project:
- The directory structure turned out to be different, because all classes must be in the folder "App_Code"
- In the “Web Site” it will not be possible to right-click on the “Controllers” directory and select Add -> Controller.
I spent some time trying to correct these shortcomings, until I came across an article where the author simply ignored them. Therefore, I decided that the implementation of these two points would be a very costly process and so far we can do without them.
Foreword
Actually all the basics outlined
here .
In order not to duplicate existing articles on the Internet, I will not describe the following questions: how to start the IIS server, how to install MVC3, and how to use it.
I will only briefly describe how the MVC library is added to an empty project, routes are created and a controller is added, which gives the results in JSON format. That is, the minimum that will then allow you to use MVC capabilities in your “Web Site” project will be considered.
It is assumed that the project will be located on the local IIS server.
')
Creating a project
- Download and install MVC3.
- Before creating a project, you need to start the IIS server. Create a new site there. Register it in the hosts file.
- We open Visual Studio as an administrator (otherwise Visual Studio will not want to post and edit a project on the IIS server).
- Select File -> New -> Web Site -> Installed Templates -> Visual C # -> Empty Web Site
- Choosing .NET Framework 4
- Choosing Web Location: HTTP
- We register url to the site we click "OK"
Project Setup
Open the "Solution Explorer". Initially, we have only one file in the project: web.config.
Add the Global.asax file, which will process all requests to the site:
Right-click on the project in the "Solution Explorer". Then the item "Add New Item ...". Choose "Global Application Class".
Since we are going to use the MVC 3 library, we will add it to the project:
Provoi button on the project in the "Solution Explorer". Then the item "Add Reference ...". Find and add System.Web.Mvc version 3.
Now if you open the web.config, then we will see that here it was registered. To make this library available in web forms and in the Global.asax file, we add the following code to the "system.web" tag:
<pages> <namespaces> <add namespace="System.Web.Mvc"/> <add namespace="System.Web.Routing"/> </namespaces> </pages>
Route Setup
Open Global.asax and insert the following code into the Application_Start () function:
AreaRegistration.RegisterAllAreas(); RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); RouteTable.Routes.MapRoute( "Default", "{controller}/{action}" );
The important thing here is that in the URL you need to write the name of the class in place of the {controller}, but without the word “Controller”, and in place of {action} the class method.
That is, if the URL is “http://site.com/Employeer/List”, then the List method of the EmployeerController class will be called.
Creating a controller
Add a directory for the “App_Code” classes:
Right-click on the project in the "Solution Explorer". Then the item "Add ASP.NET Folder". Select "App_Code".
In this directory, add the class EmployeeController.cs.
The controller must inherit from the System.Web.Mvc.Controller class.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; public class EmployeeController:Controller{ public ActionResult List() { return Json(new {ID=123,Name="Name1" }); } }
Controller start
Now, if everything is done correctly, and in the browser enter the address like "http://site.com/Employeer/List", then the server should give an error:
GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.
This means that there is some kind of protection so that json data can only be viewed from the current site. You can disable this protection:
public class EmployeeController:Controller{ public ActionResult List() { return new JsonResult { new {ID=123,Name="Name1" } , JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } }
But it is better not to disable such protection, but to let the server understand that the request comes from the same site.
To do this, create an index.html page, add jquery.js to the js folder and execute an ajax request using jquery.
Index.html:
<html> <head> <script src=" /js/jquery.js" type="text/javascript"></script> </head> <body> ddddfsf <script type="text/javascript"> $(function(){ $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", dataType: 'json', url: " /Employee/List", success: function(data) {alert(data.ID+","+data.Name);} }); }); </script> </body> </html>
Now, if you enter the site address in the browser, the server should display the index.html page and then a window with the values of the object will appear on the screen.
Conclusion
I hope this article will be useful for those who decide to use the MVC library on the Web site and maybe someone will save some time when creating their project.