ASP. NET core 2.0 some useful and interesting settings.
for (targeting) different. NET Versions:
Open the project file for ASP. NET Core 2.0: Xxx.csproj, this section:
<Sdk= "Microsoft.NET.Sdk.Web"> < PropertyGroup> <targetframework>netcoreapp2.0 </ targetframework > </ PropertyGroup >
TargetFramework is the point of the version. You can also point to multiple. NET versions, then use Targetframeworks, for example:
< Targetframeworks >netcoreapp2.0;net47</targetframeworks>
After compiling, there will be two folders under the Bin/debug folder:
Microsoft.AspNetCore.All
It's not a standard NuGet package, it doesn't contain any code or DLL, it's a metapackage, it references a lot of other packages.
Program.cs
The ASP. NET core application is actually a console application that runs an ASP. NET core-related library.
Program.cs are all configurations for hosts and environments.
Here, the default configuration is already in place.
But if you want to capture an ASP. NET Core startup error and display the error page, then:
. Capturestartuperrors (true)
Whether you should listen to Microsoft.AspNetCore.Hosting.Server.IServer-developed URLs (IPv4, V6, hostname, localhost, UNIX sockets), you can use:
. Preferhostingurls (true)
Listen for the specified URL:
. Useurls ("http://0.0.0.0:5000")
Using applicationinsights:
. Useapplicationinsights ()
Startup.cs
Startup is used to pre-load/configure services and middleware.
The Configureservices method is used to register the service.
The Configure method is used to configure the request pipeline.
Add MVC middleware, just in the Configureservices method:
Services. Addmvc ();
Using MVC Middleware, in Configure:
App. USEMVC (routes = { routes. MapRoute ("default""{controller=home}/{action=index}/{id?} " ); });
Dependency Injection
Each call creates an instance:
Services. Addtransient<iemailservice, emailservice> ();
Each HTTP request creates an instance:
Services. Addscoped<iemailservice, emailservice> ();
Create only one instance:
Services. Addsingleton<iemailservice, emailservice> ();
If you do not want the container to automatically dispose of the service (the container automatically calls the Dispose method of the service), then you should add the service manually, for example:
Services. Addsingleton (new emailservice ());
Order best practices for HTTP request Pipeline middleware:
1. Exception Handling Middleware
2. static file middleware
3. User authentication Middleware
4. MVC Middleware
Create custom Middleware
There are two ways of doing this:
1. Write directly in startup
There are four methods available: Run, Map, Mapwhen, use.
The Run method directly shorted, returning response.
Map is used to process branches, determine the beginning of a request address, add specific middleware for a branch, and so on
Mapwhen is also a processing branch, but it can control the state of the branch
Use, you can call the next middleware (next. Invoke ()) or a short-circuit request.
2. Write a class individually
Public class Communicationmiddleware { privatereadonly requestdelegate _next; Public Communicationmiddleware (requestdelegate next) { = next; } Public Async Task Invoke (HttpContext context) { await _next. Invoke (context); } }
Then write a extension method:
Public Static class communicationmiddlewareextension { publicstatic iapplicationbuilder usecommunicationmiddleware ( This Iapplicationbuilder builder) { return builder. Usemiddleware<communicationmiddleware>(); } }
Finally, in the startup configure call can:
app. Usecommunicationmiddleware (); = = { routes. MapRoute ("default""{controller=home}/{action=index}/{id?} " ); });
URL redirection and URL rewriting
They are different.
URL redirection is a loop from the server side, and then back to the client, after the client receives 301 or 302, the new address is called.
The URL rewriting is done by the server itself, and the client knows nothing about it.
You can do this by using the URL rewriting:
var New rewriteoptions () . Addrewrite ("newuser""/user/registration/index " False); App. Userewriter (options);
Exception handling
Error messages from 400 through 599 are not displayed by default. An exception page is displayed.
However, you can customize the error page:
App. Usestatuscodepages ("text/plain""HTTP error:status Code: {0} ");
Multiple environments
ASP. NET Core 2.0 loads the Appsettings.json first, and then loads the appsettings depending on the environment. {Environment}.json. Overwrite or replace some values, if necessary.
Use different startup files depending on your environment:
. Usestartup ("AssemblyName")
Startupdevelopment, Startupstaging, Startupproduction.
Three flavors of Web API
RTC, containing the name of the action
Rest, which is a best practice for HTTP protocols. The main objective is to manage and control resources.
HATEOAS, the client can dynamically navigate to the desired resource by traversing the hyper-media link inside the HTTP response, cool.
With Hateoas, you need to install the package HALCYON.MVC
ASP. NET Core 2.0 Check Gaps