Build an ASP. NET MVC site with a website (website instead of WebProject) project

Source: Internet
Author: User

Starting with the first version of ASP. NET MVC, there is only one official way to create an ASP. NET MVC project, file, new, project, and then select the ASP. NET MVC X Web application.

This approach certainly has its benefits, but many times the Web site project (WebSite) rather than the Web application (webproject) is better suited for large sites, leveraging the benefits of ASP to create more scalable sites.

In fact, ASP. NET MVC is just an extension framework of ASP, so the website project can also use MVC, and it can be used better.

In fact, in Jumony's project, the example of Jumony for MVC is a Web site project rather than a Web application.

Next, I'll use VS2010 + ASP. NET 4 (including MVC 2) to demonstrate how to use ASP.

So first, let's start by creating a solution and a website (which seems like crap), as follows:

Because I'm using a plug-in with VS, the solution browser might look different on your PC, don't worry, it's normal.

And then, in fact, everything except Global.asax and Web. config is superfluous and deleted, we get a blank site:

To continue, we add a reference to the ASP. NET MVC framework, and here I add an ASP. 2, which is actually a SYSTEM.WEB.MVC:

This reference should be visible in Web. config after adding:

Then we add a controller,controller is a type, we add a TestController type directly for the site project (note that the controller type name must end with a controller):

Then you will be prompted to put the type file in App_Code, then obviously, then the site looks like this:

Note Be sure to place the code file in App_Code, or in a third-party project that is referenced, otherwise it is not a legitimate web site project, which is completely different from webproject.

We modify the TestController class so that it inherits from the controller, deletes the default constructor, and then randomly makes an action out, so-called action is a way to return ActionResult:

The next step is to configure the route by adding a reference to another project to the Web site: System.Web.Routing, just like adding SYSTEM.WEB.MVC, no more.

Then open the Global.asax file and add the code for the route registration in the App_start method, like this:

Note the above @import instructions, if you do not have smart hints, or do not see maproute This extension method, check whether there is this import.

Adding the @import directive to each page is cumbersome, so we can configure it in Web. config slightly:

This is my modified Web. config file, and I deleted all the other things in the mess.

Next you can test the route is not correct, if you completely follow the above method, now right click on the site, choose to view in the browser, you should see such a page:

If this is a 404 error or other error, check that the Start page is set up or not, or that the Web. config configuration is incorrect.

OK, now I assume you see the same error page as I saw, this error page shows that we have successfully deployed the MVC environment, but we are missing the last thing, "view".

Next, create a views directory at the site root, then create the test subdirectory inside, adding a test.aspx file to it:

Please be careful not to tick that put the code in a separate file, that is the default check, if not unexpectedly, the current structure of the site is this:

However, this ASPX file cannot be viewed directly as an MVC view, because the MVC default Webformviewengine has some special requirements on view pages, but in fact it is very simple, just open the ASPX file and change the @page directive as follows:

Yes, the view page of MVC must inherit from ViewPage.

Then I modified the page so that it would render a hello MVC string. Then, when you choose to view the site in a browser, it should look something like this:

Here, the entire ASP. NET MVC framework was successfully deployed in the Web site project. In fact, this article is just an article to give people to fish, everyone in a step-by-year deployment of the ASP. NET MVC website (instead of WebProject), you can think about the necessity and purpose of each step. Truly know it and know why. You can also try not to put the controller in the App_Code folder, but instead place it in a third-party reference project to create a more scalable site structure.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.