Interpreting ASP.NET 5 & MVC6 Series (17): Other new features in MVC

Interpreting ASP.NET 5 & MVC6 Series (17): Other new features in MVC

(GlobalImport global import function)

In the default newly created MVC program, a new _GlobalImport.cshtml file and a _ViewStart.cshtml file are added to the Views directory. The function of this file is similar to the web.config file in the Views directory. Previously, we often set the global import namespace in this file to avoid repeating the @using xx.xx statement in each view file.

The default example is as follows:

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

The above code indicates that the BookStore and Microsoft.Framework.OptionsModel namespaces, as well as all namespaces under the Microsoft.AspNet.Mvc.TagHelpers assembly are referenced.

We have already explained the addTagHelper function in TagHelper. Note that in this example, we only referenced the BookStore namespace and did not reference the BookStore.Controllers namespace, so we cannot access the HomeController class in any view (nor can we access it as Controllers.HomeController). I hope Microsoft can improve this in the future.

Get IP related information

To obtain the IP address information of the user visitor, you can use dependency injection to obtain an instance of IHttpConnectionFeature, from which you can obtain the relevant information of the IP address. The example is as follows:

var connection1 = Request.HttpContext.GetFeature (); var connection2 = Context.GetFeature (); var isLocal = connection1.IsLocal; //Is it a 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 obtain related instances through interfaces such as IHttpRequestFeature, IHttpResponseFeature, IHttpClientCertificateFeature, and IWebSocketAcceptContext, and use the features on the instance. The above interfaces are all under the namespace Microsoft.AspNet.HttpFeature.

File upload

MVC6 provides new improvements in file upload, as shown below:

  1. < form   method = "post"   enctype = "multipart/form-data" >  
  2. < input   type = "file"   name = "files"   id = "files" multiple />  
  3. < input   type = "submit"   value = "submit"   />  
  4. </ form >  

We define the above upload form on the front-end page, and can use the new file type IFormFile in MVC6 when receiving it. The example is as follows:

[HttpPost]
public async Task Index(IList files) { foreach (var file in files) { var fileName = ContentDispositionHeaderValue .Parse(file.ContentDisposition) .FileName .Trim('"');// Beta3 version bug, the string returned by FileName contains double quotes, such as "fileName.ext" if (fileName.EndsWith(".txt"))// Only save txt files { var filePath = _hostingEnvironment.ApplicationBasePath + "\\wwwroot\\"+ fileName; await file.SaveAsAsync(filePath); } } return RedirectToAction("Index");// PRG

<<:  Interpreting ASP.NET 5 & MVC6 Series (16): Customizing View View File Search Logic

>>:  What "truths" can your brothers and sisters learn from the IT tycoons' choices of majors in the college entrance examination?

Recommend

Li Yu uses words to tell you how exciting life with ups and downs is!

This article was first published by Hunzhi Educat...

How to make products to stimulate users' desire to spread the word?

In the era of social dividends, the reason why th...

Apache Wicket 6.21.0 released, web development framework

Apache Wicket 6.21.0 released. Wicket is now deve...

In the year of the Nobel Prize for Bell's inequality, remembering Bell's life

The 2022 Nobel Prize in Physics was awarded to th...

New media operation Weibo planning plan!

Official Weibo operation plan: 1. Planning Purpos...

Are those products labeled "natural" really a waste of money?

Recently I saw a hot search "Are black sesam...

Cook’s iPhone moment has not come yet!

Click to participate in the 51CTO website content...

Quickly build an e-commerce live broadcast room from 0 to 1

The live broadcast room is the core module of e-c...