Interpretation of ASP. NET 5 & MVC6 series (17): other new features in MVC, interpretation of ASP. NET
(GlobalImport global import function)
By default, a new MVC program is added under the Views directory._GlobalImport.cshtml
File and_ViewStart.cshtml
At the same level, the file function is similar to the web. config file in the previous Views directory. Previously, we often set the global import namespace in this file to avoid repeated use in each view File.@using xx.xx
Statement.
The default example is as follows:
@using BookStore@using Microsoft.Framework.OptionsModel@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"
The code above indicates that referenceBookStore
AndMicrosoft.Framework.OptionsModel
Namespace, andMicrosoft.AspNet.Mvc.TagHelpers
All namespaces under the Assembly.
We have explained the addTagHelper function in TagHelper.
Note: In this example, we only referenceBookStore
Namespace, not referencedBookStore.Controllers
Namespace, so we cannot access any viewHomeController
Class (cannot startControllers.HomeController
In the future.
Obtain IP information
To obtain the IP address information of a visitor, you can use dependency injection to obtainIHttpConnectionFeature
To obtain information about the IP address. The instance is as follows:
Var connection1 = Request. httpContext. getFeature <IHttpConnectionFeature> (); var connection2 = Context. getFeature <IHttpConnectionFeature> (); var isLocal = connection1.IsLocal; // whether the local IP address var localIpAddress = connection1.LocalIpAddress; // The 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 useIHttpRequestFeature
,IHttpResponseFeature
,IHttpClientCertificateFeature
,IWebSocketAcceptContext
To use the features of the instance.Microsoft.AspNet.HttpFeature
.
File Upload
MVC6 provides new improvements in file upload, for example:
<form method="post" enctype="multipart/form-data"> <input type="file" name="files" id="files" multiple /><input type="submit" value="submit" /></form>
We define the above upload form on the front-end page. The new file type in MVC6 can be used for receivingIFormFile
, For example:
[HttpPost] public async Task <IActionResult> Index (IList <IFormFile> files) {foreach (var file in files) {var fileName = ContentDispositionHeaderValue. parse (file. contentDisposition ). fileName. trim ('"'); // beta3 bug. The string returned by FileName contains double quotation marks, such as" fileName. ext "if (fileName. endsWith (". txt ") // only save the txt file {var filePath = _ hostingEnvironment. applicationBasePath + "\ wwwroot \" + fileName; await file. saveAsAsync (filePath) ;}return RedirectToAction ("Index"); // PRG}