Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the new features in MVC

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you about the new features of MVC. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

(GlobalImport global import function)

By default, in the newly created MVC program, a new _ GlobalImport.cshtml file and _ ViewStart.cshtml level have been added in the Views directory. The function of this file is similar to the web.config file in the previous Views directory. We often set the global import namespace in this file to avoid repeated @ using xx.xx statements in each view file.

The default examples are as follows:

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

The above code represents a reference to the BookStore and Microsoft.Framework.OptionsModel namespaces, as well as all namespaces under the Microsoft.AspNet.Mvc.TagHelpers assembly.

We have already explained the function of addTagHelper in TagHelper

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

Get IP related information

To obtain information about the IP address of a user visitor, you can use dependency injection to obtain an instance of IHttpConnectionFeature, from which you can obtain information about the IP address, as shown below:

Var connection1 = Request.HttpContext.GetFeature (); var connection2 = Context.GetFeature (); 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 use the features on the instance by obtaining related instances through IHttpRequestFeature, IHttpResponseFeature, IHttpClientCertificateFeature, IWebSocketAcceptContext and other interfaces, all of which are under the namespace Microsoft.AspNet.HttpFeature.

File upload

MVC6 provides new and improved processing for file upload, such as:

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

[HttpPost] public async Task Index (IList files) {foreach (var file in files) {var fileName = ContentDispositionHeaderValue .Parse (file.ContentDisposition) .filename .trim ('"); / / the string returned by the beta3 version of bug,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} these are the new features of MVC shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report