Interpretation of ASP.net 5 & MVC6 series: Other new features in MVC _ self-study process

Source: Internet
Author: User

(Globalimport global Import feature)

By default, the newly created MVC program, in the Views directory, adds a new _GlobalImport.cshtml file and a _ViewStart.cshtml peer, The function of this file is similar to the Web.config file in the previous views directory, where we often set the global import namespace in the file to avoid reusing 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 above code represents, references BookStore and Microsoft.Framework.OptionsModel namespaces, and Microsoft.AspNet.Mvc.TagHelpers all namespaces under the assembly.

As for the Addtaghelper function, we've already explained it in Taghelper.

Note that in this case, we only refer to BookStore namespaces and do not reference BookStore.Controllers namespaces, so we cannot access the class in any view (nor can we HomeController access it in Controllers.HomeController the form of it) and hope that Microsoft can improve it later.

Get IP-related information

To obtain information about the IP address of a user visitor, you can use dependency injection to obtain IHttpConnectionFeature an instance from which you can obtain information about the IP address, 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 IHttpRequestFeature get the relevant instance through,,, and so on, using the attributes on that instance, all of which are under the IHttpResponseFeature IHttpClientCertificateFeature IWebSocketAcceptContext namespace Microsoft.AspNet.HttpFeature .

File Upload

MVC6 in the file upload aspect, has given the new improvement processing, for example as follows:

<form method= "POST" enctype= "Multipart/form-data" > <input "Files" type= "Files" name= "
  file" >
<input type= "Submit" value= "Submit"/>
</form>

We define the above upload form in the front-end page, and receive the new file types in the MVC6 that can be used in the IFormFile following example:

[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 returns a string that contains double quotes, such as" filename.ext "
    if (filename.endswith (" TXT "))//Save TXT file
    only {
      var filePath = _hostingenvironment.applicationbasepath + "\\wwwroot\\" + fileName;
      await file. Saveasasync (FilePath);
    }
  Return redirecttoaction ("Index");//PRG
}

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.