ASP. NET MVC start customizing projects from empty projects

Source: Internet
Author: User

In the previous article on net core, I've already talked about how to build websocket from scratch.

Today, we talk about the ASP. NET file structure, how to organize the files in your project with your own directory structure.

If you use the visual Studio (VS) Wizard or the Dotnet Wizard, a set of MVC common frameworks will be generated for us. However, for a project that requires more special or smaller, it may not be as we wish. Mixed-case file names, sometimes less suited to systems like Linux, at least I feel the same way. The directory hierarchy too Deep is also quite annoying thing (exaggerated design, like the Nodejs of the Windows address the maximum length of the full is really scary).

Suppose we just need a simple page, a simple template relationship to complete a small site, we can do the following.

1) Create an empty project to start the file.

In the startup file, the general is startup.cs, to have such a section of your startup class. I merged the wizard-produced program and startup, which is more appropriate for our goal.

usingMicrosoft.aspnetcore;usingMicrosoft.AspNetCore.Builder;usingMicrosoft.AspNetCore.Hosting;usingMicrosoft.Extensions.Configuration;usingMicrosoft.Extensions.DependencyInjection; Public classprogram{ Public Static voidMain (string[] args) {Webhost.createdefaultbuilder (). Usestartup<Startup>()            . Build ().    Run (); }} Public classstartup{ PublicStartup (iconfiguration configuration) {Configuration=configuration; }     PublicIConfiguration Configuration {Get; }  Public voidconfigureservices (iservicecollection services) {services.    Addmvc (); }     Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env) {if(env. Isdevelopment ()) {app.            Usebrowserlink (); App. Usedeveloperexceptionpage ();//using the developer exception page        }        Else{app. Useexceptionhandler ("/error");//using the exception handler} app. Usestaticfiles ();//Static file Supportapp.    Usemvc (); }}

In fact, is to add MVC support, static file support, let Wwwroot directory work, this directory name does not study how to change the name, the meaning is not big.

USEMVC, use MVC.

Exception Handling Page "/error" Just customize it yourself, at least after you read this article.

2) Add the home controller.

Add a file, Home.cs

Well, it's more like a little project style!

The file name is unlimited, the namespace is not limited (there is no can also), the class name is not limited, super cool is it, the degree of freedom is very high.

Instead of registering your route, use the route attribute directly to describe how your path corresponds. [Route ("")] is the root directory. Or it could be home/index. You can also have multiple route attribute descriptions.

You can use the Controller::ok method to directly return the HTTP corresponding code of 200 content, the return value must be iactionresult. It can also be a string return value.

You can also do this:

Then add a razor view (index.cshtml) so that the view can be placed anywhere in the project without the views directory:

< Body >   < Div > Any Content </div></body>

Razor view can still inherit _viewstart.cshtml,linux under note case. You can add @{layout = "_layout.cshtml";} Directly use the template under the root directory.

You can add data to Controller::viewdata in the controller, viewdata["title" = "Some description". can be accessed directly in the view. @ViewData ["title"].

@{    Layout = "_layout.cshtml";} < Body >    < Div >@ViewData ["title"]</div></body  >

The knowledge of the template is not spoken here, the relationship is not big.

3) can be run.

So we have a very concise MVC project.

ASP. NET MVC start customizing projects from empty projects

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.