Original: Interpretation of the ASP 5 & MVC6 series: 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; //是否本地IP var localIpAddress = connection1.LocalIpAddress; //本地IP地址var localPort = connection1.LocalPort; //本地IP端口var remoteIpAddress = connection1.RemoteIpAddress; //远程IP地址var remotePort = connection1.RemotePort; //本地IP端口
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 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]public async Task<IActionResult> Index(IList<IFormFile> files){ foreach (var file in files) { var fileName = ContentDispositionHeaderValue .Parse(file.ContentDisposition) .FileName .Trim('"');// beta3版本的bug,FileName返回的字符串包含双引号,如"fileName.ext" if (fileName.EndsWith(".txt"))// 只保存txt文件 { var filePath = _hostingEnvironment.ApplicationBasePath + "\\wwwroot\\"+ fileName; await file.SaveAsAsync(filePath); } } return RedirectToAction("Index");// PRG}
Synchronization and recommendations
This article has been synchronized to the Catalog index: Interpreting ASP. & MVC6 Series
Interpreting the ASP. NET 5 & MVC6 series: Other new features in MVC