Interpretation of ASP. NET 5 MVC 6 Series Tutorials (17): Other New Features in MVC
- 2021-07-26 07:27:35
- OfStack
(GlobalImport Global Import Feature)
In the newly established MVC program by default, one is newly added under the Views directory
_GlobalImport.cshtml
Documents and
_ViewStart.cshtml
At the same level, the function of this file is similar to the previous web. config file in the Views directory, where we often set the namespace of global import to avoid reuse 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 above code indicates that the reference
BookStore
And
Microsoft.Framework.OptionsModel
Namespace, and
Microsoft.AspNet.Mvc.TagHelpers
All namespaces under the assembly.
We have already explained the functions of addTagHelper in TagHelper
Note that in this example, we only refer to
BookStore
Namespace, and does not reference
BookStore.Controllers
Namespace, so we can't access it in any view
HomeController
Class (you can't use the
Controllers.HomeController
I hope Microsoft can improve it in the future.
Get information about IP
To obtain information about IP addresses of user visitors, you can use dependency injection to obtain
_ViewStart.cshtml
0
From which information about the IP address can be obtained, as follows:
var connection1 = Request.HttpContext.GetFeature<IHttpConnectionFeature>();
var connection2 = Context.GetFeature<IHttpConnectionFeature>();
var isLocal = connection1.IsLocal; // Whether 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 also pass the
IHttpRequestFeature
,
IHttpResponseFeature
,
IHttpClientCertificateFeature
,
IWebSocketAcceptContext
And so on, get the related instance, so as to use the characteristics on the instance, all of which are in the namespace
Microsoft.AspNet.HttpFeature
Below.
File upload
MVC6 gives new improvements in file uploading, such as the following:
<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 we can use the new file type in MVC6 when receiving it
IFormFile
Examples are 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 Version of bug , FileName The returned string contains double quotation marks, such as "fileName.ext"
if (fileName.EndsWith(".txt"))// Save Only txt Documents
{
var filePath = _hostingEnvironment.ApplicationBasePath + "\\wwwroot\\"+ fileName;
await file.SaveAsAsync(filePath);
}
}
return RedirectToAction("Index");// PRG
}