Interpretation of ASP. NET 5 & MVC6 series (17): other new features in MVC, interpretation of ASP. NET

Source: Internet
Author: User

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.cshtmlFile and_ViewStart.cshtmlAt 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.xxStatement.
The default example is as follows:

@using BookStore@using Microsoft.Framework.OptionsModel@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"

The code above indicates that referenceBookStoreAndMicrosoft.Framework.OptionsModelNamespace, andMicrosoft.AspNet.Mvc.TagHelpersAll namespaces under the Assembly.

We have explained the addTagHelper function in TagHelper.

Note: In this example, we only referenceBookStoreNamespace, not referencedBookStore.ControllersNamespace, so we cannot access any viewHomeControllerClass (cannot startControllers.HomeControllerIn the future.

Obtain IP information

To obtain the IP address information of a visitor, you can use dependency injection to obtainIHttpConnectionFeatureTo 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,IWebSocketAcceptContextTo 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}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.