Other new features in MVC (Globalimport Global import feature)
The default newly created MVC program, in the Views directory, adds a new _GlobalImport.cshtml file and a _ViewStart.cshtml peer, This file functions like the Web. config file in the previous views directory, and we often set the global import namespace in that file to avoid repeating the statements in each view file @using xx.xx .
The default example is as follows:
@using BookStore@using Microsoft.Framework.OptionsModel@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"
The preceding code represents the reference BookStore and Microsoft.Framework.OptionsModel namespace, and Microsoft.AspNet.Mvc.TagHelpers all namespaces under the assembly.
About the Addtaghelper feature, we've already explained it in Taghelper.
Note that in this case we only refer to the BookStore namespace and do not reference the namespace BookStore.Controllers , so we cannot access the class in any view HomeController (nor access it in Controllers.HomeController the form), and hopefully Microsoft will be able to improve it later.
Get IP-related information
To obtain information about the IP address of a user's visitor, you can take a dependency injection and obtain IHttpConnectionFeature an instance from which you can obtain information about the IP address, as shown in the following example:
var Connection1 = Request.httpcontext.getfeature<ihttpconnectionfeature> (); var Connection2 = context.getfeature<ihttpconnectionfeature> (); var isLocal = Connection1. IsLocal; //whether the local IP var localipaddress = Connection1. LocalIpAddress; //local IP address var localport = Connection1. LocalPort; //local IP Port var remoteipaddress = Connection1. remoteipaddress; //remote IP address var remoteport = Connection1. RemotePort; //local IP port
Similarly, you can get related instances through,,,, and so on, IHttpRequestFeature IHttpResponseFeature using the IHttpClientCertificateFeature IWebSocketAcceptContext attributes on that instance, which are below the namespace Microsoft.AspNet.HttpFeature .
File Upload
MVC6 in the file upload aspect, gave the new improvement processing, the example is as follows:
<form method=" post "enctype= "Multipart/form-data" > <input Span class= "Hljs-attribute" >type= "file" name=" files "id=" files " multiple/><input type= "submit" value= "submit"/></FORM>
We define the above upload form on the front page and receive the new file type that can be used in MVC6 IFormFile , as follows:
[HttpPost]PublicAsync task<iactionresult>index (ilist<iformfile> files) { foreach (var file in files) {var fileName = Contentdispositionheadervalue. Parse (file. contentdisposition). FileName. Trim (if (Filename.endswith (") //save only TXT file {var FilePath = _ Hostingenvironment.applicationbasepath + "\\wwwroot\\" + fileName; await file. Saveasasync (FilePath); }} return redirecttoaction ( "Index");
Synchronization and recommendations
This article has been synchronized to the Catalog index: Interpreting ASP. & MVC6 Series
Other new features in MVC